Google表单脚本 - 电子邮件提交

时间:2015-10-27 13:02:23

标签: google-apps-script google-sheets google-docs google-spreadsheet-api

我试图让谷歌通过电子邮件向我发送表格中提交的信息。我可以使用默认工作表(表单响应)但我使用一个名为" ConversionTracker"的单独表格。实际上使用查询功能将数据从默认工作表导入到第二个工作表,以便我可以在响应中使用公式。基本上我希望电子邮件脚本在发送上次提交的电子邮件时引用第二个工作表而不是第一个工作表。每次我使用脚本时我都会从第一个工作表发送数据(表单响应)而不是工作表命名"转换跟踪器"我尝试过不同的剧本版本,但我似乎无法得到它。见下面的脚本。

function Initialize() {

 try {

var triggers = ScriptApp.getProjectTriggers();

for (var i in triggers)
  ScriptApp.deleteTrigger(triggers[i]);

ScriptApp.newTrigger("EmailGoogleFormData")
  .forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
  .onFormSubmit().create();

 } catch (error) {
throw new Error("Please add this code in the Google Spreadsheet");
 }
 }

  function EmailGoogleFormData(e) {

if (!e) {
  throw new Error("Please go the Run menu and choose Initialize");
}

  try {

if (MailApp.getRemainingDailyQuota() > 0) {


  // You may replace this with another email address
  var email = "dustin.****@b****.com";

  // Enter your subject for Google Form email notifications
  var subject = "Google Form Submitted";

  var key, entry,
    message = "",
    ss = SpreadsheetApp.getActiveSheet(),
    sheet = ss.getSheets()["ConversionTracker"],
    ss.setActiveSheet(sheet),
    cols = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0];

  // Iterate through the Form Fields
  for (var keys in cols) {

    key = cols[keys];
    entry = e.namedValues[key] ? e.namedValues[key].toString() : "";

    // Only include form fields that are not blank
    if ((entry !== "") && (entry.replace(/,/g, "") !== ""))
      message += key + ' :: ' + entry + "\n\n";
  }

  MailApp.sendEmail(email, subject, message);
}
 } catch (error) {
   Logger.log(error.toString());
 }
   }

工作并从第一张工作表发送数据的脚本是:

  

function Initialize(){       试试{

var triggers = ScriptApp.getProjectTriggers();

for (var i in triggers)
  ScriptApp.deleteTrigger(triggers[i]);

ScriptApp.newTrigger("EmailGoogleFormData")
  .forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
  .onFormSubmit().create();
} catch (error) {
throw new Error("Please add this code in the Google Spreadsheet");   } }   function EmailGoogleFormData(e) {
if (!e) {
throw new Error("Please go the Run menu and choose Initialize");   }
try {

if (MailApp.getRemainingDailyQuota() > 0) {

  // You may replace this with another email address
  var email = "dustin****@b****.com";

  // Enter your subject for Google Form email notifications
  var subject = "Google Form Submitted";

  var key, entry,
    message = "",
    ss = SpreadsheetApp.getActiveSheet(),
    cols = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0];

  // Iterate through the Form Fields
  for (var keys in cols) {

    key = cols[keys];
    entry = e.namedValues[key] ? e.namedValues[key].toString() : "";

    // Only include form fields that are not blank
    if ((entry !== "") && (entry.replace(/,/g, "") !== ""))
      message += key + ' :: ' + entry + "\n\n";
  }

  MailApp.sendEmail(email, subject, message);
}   } catch (error) {
Logger.log(error.toString());   } }

1 个答案:

答案 0 :(得分:0)

我不认为你可以使用方括号内的文字来获取表格:

sheet = ss.getSheets()["ConversionTracker"]

我使用.getSheetByName()方法。

getSheetByName("ConversionTracker");

代码:

ss = SpreadsheetApp.getActiveSpreadsheet();
sheet = ss.getSheetByName("ConversionTracker");