Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.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
Javascript Google Apps根据电子表格中的值在自定义日期和时间为基于时间的触发器编写脚本_Javascript_Google Apps Script_Google Sheets_Triggers_Google Apps Script Api - Fatal编程技术网

Javascript Google Apps根据电子表格中的值在自定义日期和时间为基于时间的触发器编写脚本

Javascript Google Apps根据电子表格中的值在自定义日期和时间为基于时间的触发器编写脚本,javascript,google-apps-script,google-sheets,triggers,google-apps-script-api,Javascript,Google Apps Script,Google Sheets,Triggers,Google Apps Script Api,我正在尝试创建一个谷歌应用程序脚本,它能够根据电子表格中的值在特定时间安排邮件。将有多个日期时间单元格,根据这些单元格进行触发。我曾尝试使用ScriptApp.newTrigger()以编程方式添加触发器,但我不知道该函数何时创建并运行。任何关于如何实现这一点的见解都将是非常棒的 编辑: 当用户像这样提交表单时创建触发器 function saveForm(form) { const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheet

我正在尝试创建一个谷歌应用程序脚本,它能够根据电子表格中的值在特定时间安排邮件。将有多个日期时间单元格,根据这些单元格进行触发。我曾尝试使用
ScriptApp.newTrigger()
以编程方式添加触发器,但我不知道该函数何时创建并运行。任何关于如何实现这一点的见解都将是非常棒的

编辑: 当用户像这样提交表单时创建触发器

function saveForm(form) {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Test');

  const now = Utilities.formatDate(new Date(), "GMT+5:30", "yyyy-MM-dd");

  const newRow = [now, form.cid, form.custName, form.custNumber, form.goodsType, form.tonnage, form.area, form.completeAddr, `${now} - ${form.time}`, form.comments];
  sheet.appendRow(newRow); 

  const customDate = new Date(`${now}T${form.time}`);

  ScriptApp.newTrigger('dispatchMail').timeBased().at(customDate).create();


  return 'Data saved successfully';
}
现在是发送邮件的功能

  function dispatchMail() {
       const now = Utilities.formatDate(new Date(), "GMT+5:30", "yyyy-MM-dd");

      const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Test');
      const data = sheet.getDataRange().getValues();

      const normalized = {
               'Mark': [],
               'Jack': [],
               'Chloe': [],
               'Robin': [],
               'Unassigned': []
      };

      for(let i=1; i<data.length; i++) {
          normalized[data[i][10]] = normalized[data[i][10]].concat([data[i]]);
      }

     //start our mail dispatch here once we have aggregated the data
  }
函数dispatchMail(){
const now=Utilities.formatDate(新日期(),“GMT+5:30”,“yyyy-MM-dd”);
const sheet=SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Test');
const data=sheet.getDataRange().getValues();
常数归一化={
“标记”:[],
“杰克”:[],
“克洛伊”:[],
“罗宾”:[],
“未分配”:[]
};

对于(设i=1;i如果您的问题与确定触发器运行时应执行的操作有关,您可能需要更改处理方法

与其为每条消息创建一个触发器,不如在需要发送消息时进行一次触发器检查

你可以:

  • 创建一个函数:
    • 过滤掉已经发送的消息
    • 查找过去有
      目标发送时间的邮件
    • 发送这些信息
  • 创建基于时间的触发器以运行该函数
请记住,在最佳情况下,您的邮件将在目标时间发送,在最坏情况下,您的邮件将在
时间发送


此外,您还需要跟踪已发送的内容(例如,使用额外的列或删除行)

Hello@FatehAK,你介意分享关于你的问题的代码吗?干杯!嗨@ale13我添加了一些代码供参考Hello@FatehAK,请让我清楚地了解-你想根据日期向不同的人发送电子邮件吗?干杯!根据日期和时间@ale13