需要使用googleappscript编辑gdoc [英] Need to edit gdoc using a googleappscript

查看:107
本文介绍了需要使用googleappscript编辑gdoc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将gdoc的正文添加到电子邮件中?我有点知道如何做,但是我不确定.我已经在下面编写了这段代码以帮助我.我对此并不陌生,我设法运行了一些脚本,但是我完全迷失了这个脚本.我看了几个视频,这就是我所能做的.代码如下. 基本上,我想做的是让用户输入他的名字和另一个变量,然后转到google doc文件,并将其更改为输入的值,然后将其放回电子邮件中,然后将其发送到解决...关于我做错了什么或应该从哪里开始的任何想法?预先感谢.

Is it possible that I could add the body of a gdoc into an email? I kinda have an idea of how to do it but I am not completely sure. I have written this code below to kinda help me. I am new to this and I have managed to have a few scripts running, but I am completely lost on this one. I have watched several videos and this is what I was able to do. The code is below. Basically what I want to do is to be able to have a user input his name and another variable and then go to the google doc file and change it to the value that was input and then put it back in an email and send it to an address... Any ideas of what I am doing wrong or where should I start?? Thanks in advance.

function gsnot() {
var emailaddress="albdominguez25@gmail.net";
var sub="Subject1";
var pattern = Browser.inputBox("Enter your name");
var pattern2 = Browser.inputBox("Enter the minutes:");
var templateDocID= ScriptProperties.getProperty("EmailTemplateDocId");
var doc = DocumentApp.openById(templateDocID);
  var body = doc.getActiveSection()
var html = "";


var keys = {
name: pattern, 
min: pattern2,
};

for ( var k in keys ) {
body.replaceText("%" + k + "%", keys[k]);

doc.saveAndClose();
html = getDocAsHtml(docId);
DocsList.getFileById(docId).setTrashed(true);

return html;

var emailaddress="albdominguez25@gmail.net";
var sub="Subject1";

MailApp.sendEmail(emailaddress,sub, {htmlBody: body});}}

推荐答案

您可能想要更改代码,方法是将文档的正文读取到变量中,对变量进行替换,然后将其插入电子邮件中.例如:

You might want to change your code by reading the body from the document into a variable, doing the replace on the variable and inserting that into your email. For example:

function gsnot() {
  var emailaddress = "albdominguez25@gmail.net";
  var sub = "New Subject";
  var pattern  = Browser.inputBox("Enter your name:");
  var pattern2 = Browser.inputBox("Enter the minutes:");
  var templateDocID = ScriptProperties.getProperty("EmailTemplateDocId");
  var doc  = DocumentApp.openById(templateDocID);
  var body = doc.getText();
  var replacement;
  var k;

  var keys = {
    name: pattern, 
    min:  pattern2
  };

  for (k in keys) {
    if (keys.hasOwnProperty(k)) {
      replacement = new RegExp("%" + k + "%",'g');
      body = body.replace(replacement, keys[k]);
    }
  }
  MailApp.sendEmail(emailaddress,sub, '', {htmlBody: body});
}

一些注意事项:

  • 将所有var语句放在开头是一种很好的形式 函数
  • 当使用for-in时(例如for(键中的k)),它将返回对象的所有属性.您只想要分配的那些.这是以下原因的原因:for(键中为k)
  • 您已经为每个属性发送了邮件,我相信您希望它在for-in循环之外,因此仅在所有替换完成后才发送.
  • 使用replace(),您需要创建一个设置为global的正则表达式对象 否则它将仅替换模式的第一个实例(您有两次命名).
  • 即使在使用htmlBody选项的情况下,在sendEmail()的参数中,也需要指定纯文本主体.我用空引号''.
  • It is good form to have all your var statements at the beginning of the function
  • When you use for-in (eg. for (k in keys) ), it returns all properties of the object. You only want the ones you assigned. This is the reason for: for (k in keys)
  • You had the mail sending for each property, I believe you wanted it outside the for-in loop so it only sent after all the replacements were completed.
  • Using replace(), you need to create a regular expression object that is set to global or it will only replace the first instance of the pattern (you have name twice).
  • In your parameters for sendEmail(), even if you are using the htmlBody option, you need to specify a plain text body. I used empty quotes ''.

这篇关于需要使用googleappscript编辑gdoc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆