Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 找不到方法sendEmail(对象)_Google Apps Script_Gmail Api - Fatal编程技术网

Google apps script 找不到方法sendEmail(对象)

Google apps script 找不到方法sendEmail(对象),google-apps-script,gmail-api,Google Apps Script,Gmail Api,尝试在Google Sheets上编写脚本时,我遇到了一个错误: 找不到方法sendEmailobject 对于以下代码。正如你所理解的,这是一封介绍新客户的电子邮件,向客户介绍他的客户代表 var clientname = spreadsheet.getRange('C2').getValue(); var clientemail = spreadsheet.getRange('P2').getValue(); var repname = spreadsheet.getRange(

尝试在Google Sheets上编写脚本时,我遇到了一个错误:

找不到方法sendEmailobject

对于以下代码。正如你所理解的,这是一封介绍新客户的电子邮件,向客户介绍他的客户代表

  var clientname = spreadsheet.getRange('C2').getValue();
  var clientemail = spreadsheet.getRange('P2').getValue();
  var repname = spreadsheet.getRange('H2').getValue();
  var repmobile = spreadsheet.getRange('AA2').getValue();
  var repemail = spreadsheet.getRange('AB2').getValue();

     GmailApp.sendEmail({
        to: clientemail,
        subject: EMAIL_SUBJECT,
        htmlBody: createEmailBody(clientname, repname, repmobile, repemail),
        name: 'myname', 
        from: 'welcome@mydoamin.com', 
        replyTo: repemail
      });
多亏了你们的大力帮助,我现在将代码更改为:

GmailApp.sendEmail({
    recipient: clientemail, 
    subject: email_subject, 
    htmlBody: createEmailBody(clientname.toString(), repname.toString(), repmobile.toString(), repemail.toString()),
    {name: 'myname',
    from: 'welcome@mydomain.com', 
    replyTo: repemail}
  });
现在的错误是:

无效的属性ID


在以{name

开头的行中,根据您的要求修改此代码

代码是从官方文档中复制的。正确的方法是:

function myFunction() {
// The code below will send an email with the current date and time.
var now = new Date();
GmailApp.sendEmail("email@example.com", "current time", "The time is: " + now.toString()); 
}
按要求编辑:HTML正文示例:

function myFunction() {
// The code below will send an email with the current date and time.
var now = new Date();
GmailApp.sendEmail("email@example.com", "current time", "The time is: " + now.toString(),{htmlBody: '<h1>hello</h1>'});
  }

以下是补充答覆:

您需要学习理解错误消息-在调试时,他们是您最好的朋友,但他们并不总是直观的。在您的情况下,错误状态为:找不到方法sendEmailobject。请尝试将其作为模板字符串进行解析,消息显示:

找不到方法 搜索的方法是sendEmail 它作为单个参数传递对象的实例 由于在确保没有输入错误后,显然有一种方法称为sendEmail,因此您可以安全地删除前两部分。这就给我们留下了object实例—一个自然的问题是,是否允许将对象传递给此方法?如果您查看文档,则不允许

但是你可以从一开始就避免所有这些。我发现有一篇博客文章非常有用——关于GAS插件开发。第一篇文章击中了一个苹果——你最好不要使用脚本编辑器,安装一个支持自动完成的or编辑器,无论是与代码、Netbeans还是其他工具相比

也就是说,重复其他答案,方法签名的结构如下:

sendEmail( recipient, subject, body, options ) ________________________________________________ name arg0 arg1 arg2 [arg3] 请注意,前三个参数是强制性的,只有第四个参数是可选的。 如果您再次查看参考文档,所有高级参数都位于选项中

注释

在花括号中:我假设您知道这一点,但以防万一:这是一个用于实例化对象的对象文字初始值设定项。如果方法签名表明不需要将所有参数包装在一个对象中,那么我认为您的做法是对的,不过-最好使用单个配置参数声明API,只要不要违反公共合同。 参考资料

发送带有选项的电子邮件 发送不带选项的电子邮件 MailApp方法sendEmail 解决方案 您未正确使用,因为您将对象作为单个参数而不是正确的参数传递。根据上面链接的文档,您的参数应为以下参数:

收件人字符串 主题字符串 身体线 选项对象 因此,您的方法最终将如下所示:

GmailApp.sendEmailclientemail,电子邮件主题,createEmailBodyclientname.toString,repname.toString,repmobile.toString,repemail.toString{ 姓名:'我的姓名', 发件人:'welcome@mydomain.com', 回复:repemail}
;可能是因为您的参数不正确。但我没有检查。我认为您想使用MailApp。您正在使用MailApp的签名。GmailApp有不同的签名!我使用GmailApp只是因为它能够将from更改为我的一个别名。据我所知,MailApp无法做到这一点。是的。它可以工作吗er修复参数?不,它不起作用:@user13708028您不必编写收件人、主题。您只需直接编写它们而不指定。只有像htmlBody和其他高级参数在花括号{}中显式指定。请正确查看我的第二个示例。谢谢,但是我如何使用htmlBody?我无法将其与我列出的参数一起使用?已更新。请立即检查。好的。那么您的要求是什么?这看起来是一篇写得非常好的文章。如果您更改了我粘贴的代码中的电子邮件地址,它将在应用程序脚本中工作。如果您是从wi执行此操作的薄的电子表格,可能还有其他事情要做。那/那将是另一个问题s@user13708028-显示您所做的更改—它从来没有像不工作一样简单—请具体说明到底是什么错误,预期是什么,等等。请不要覆盖原始问题,将更改附加到它,无需编辑/更新日期标题-您可以使用--[三个破折号]分隔部分