【问题标题】:Automated emailing using information from a google form and different spreadsheets?使用来自谷歌表单和不同电子表格的信息自动发送电子邮件?
【发布时间】:2020-03-19 03:32:05
【问题描述】:

我是谷歌脚本的新手,所以请多多包涵。我正在使用谷歌表格和谷歌表格自动进行 2 条件研究注册的通信。本质上,当参与者提交表单时,我需要发送一封确认电子邮件(触发是表单提交),其中包含他们选择的日期,以及基于他们的条件的新唯一 ID(我有两个单独的预制 ID 列表我需要使用)。现在,我有主表单响应表,然后我使用 = 查询根据调查中的多项选择问题(“早上”和“晚上”)将调查响应分成两个条件的表。我将 ID 列表填充到相应的工作表中,以便当 = 查询对响应进行排序时,它们会在行填充时自动“分配”一个 ID。我的代码可以在没有 ID 的情况下发送电子邮件,只使用电子表格第一张表(表单响应表)中的名称和日期,但是在从另一个相应的表中获取 ID 以放入电子邮件时遇到问题.

这是我所拥有的:

  function IntialEmails(e){
  var ss = SpreadsheetApp.openById('MyownSpreadsheetID'); //not including real ID for privacy reasons
  var type = e.values[4];

  if (type == 'Condition A') {
    var sheet = ss.getSheets()[1];
    ss.setActiveSheet(sheet);
  }

  if (type == 'Condition B') {
    var sheet = ss.getSheets()[2]; 
    ss.setActiveSheet(sheet);
  }


  var userName = sheet.values[0];
  var userEmail = sheet.values[2];
  var date = sheet.values[4]; //Using date from corresponding spreadsheet based on survey format
  var ID = sheet.values[5]; // Should be from corresponding sheet
  var subject = "Study Signup Confirmation and Participant ID";

  var templ = HtmlService
      .createTemplateFromFile('My html'); //not including real ID for privacy reasons

  templ.date = date;
  templ.ID = ID;
  templ.userName = userName;

  var message = templ.evaluate().getContent();

  MailApp.sendEmail({
    to: userEmail,
    subject: subject,
    htmlBody: message
  });

}

我非常卡住,并且不断出错。此代码返回错误“TypeError:无法读取未定义的属性“0””如果有人可以帮助我,将不胜感激!

【问题讨论】:

  • 你的模板文件是什么样的?
  • 你的身份证是什么样子的?
  • 哪一行会出现这个错误?

标签: google-apps-script google-sheets


【解决方案1】:

如果您想从工作表中获取值,您可以执行以下操作:(即使我通常不会这样做,但我有点遵循您使用的相同方法。我假设你有你的 id 或任何行中的数据。但显然我不知道,因为你没有提供这些信息。

function initEmails(e) {
  var ss=SpreadsheetApp.getActive();
  var shts=ss.getSheets();
  var sh=(e.values[4]=="Condition A")?shts[1]:shts[2];
  var vs=sh.getRange(1,1,sh.getLastRow(),7).getValues();
  for(var i=0;i<vs.length;i++) {
    if(vs[i][7]!='USED') {
       var userName=vs[i][0];
       var userEmail=vs[i][2];
       var date=vs[i][4];
       var ID=vs[i][5];
       var subject = "Study Signup Confirmation and Participant ID";
       sh.getRange(i+1,8).setValue('USED');//this provides a way to keep track of which row was used last
    } 
    /*.............I have no idea what your trying to do beyond this point.  

  }

但我认为您无法通过这种方式将这些值插入到您的 html 模板中:

templ.date = date;
templ.ID = ID;
templ.userName = userName;

通常,模板会在评估过程中使用 scriptlet 填充所需的数据。

Templated HTML

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多