简体   繁体   English

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

[英]Google Apps Script Time based trigger at custom date and time based on value in spreadsheet

I am trying to create a Google apps script with the ability to schedule mails at specific times based on the value in the spreadsheet.我正在尝试创建一个 Google 应用程序脚本,该脚本能够根据电子表格中的值在特定时间安排邮件。 There will be multiple date-time cells based on which the trigger would be made.将有多个日期时间单元格基于这些单元格进行触发。 I have tried adding the trigger programmatically using ScriptApp.newTrigger() but I don't get when that function the creates it should run.我曾尝试使用ScriptApp.newTrigger()编程方式添加触发器,但我不知道该函数何时应该运行。 Any insight on how to go about this would be great.关于如何解决这个问题的任何见解都会很棒。

Edit: Created the trigger when the user submits the form like this.编辑:当用户像这样提交表单时创建触发器。

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';
}

Now the function that dispatches the mail现在发送邮件的功能

  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
  }

But now the problem is won't have any idea of whom to dispatch the Mail to.但是现在的问题是不知道将邮件发送给谁。 eg.例如。 The user submits the form with Mail Trigger scheduled for Mark at 2pm and Jack at 4pm.用户提交表单,邮件触发器计划在下午 2 点发送给马克,下午 4 点发送给杰克。 Now in the dispatch mail function how to determine that this mail is to dispatched to Mark or Jack.现在在发送邮件函数中如何确定该邮件是发送给马克还是杰克。 I don't see a way of passing params to the trigger function.我没有看到将参数传递给触发器函数的方法。 Any way to go about this?有什么办法可以解决这个问题吗?

If your problem is related to identifying what should be executed when the trigger runs, you may want to change how you approach this.如果您的问题与确定触发器运行时应执行的内容有关,您可能需要更改处理方式。

Instead of creating one trigger for each message, why not have a single trigger check if messages need to be sent.与其为每条消息创建一个触发器,不如让一个触发器检查是否需要发送消息。

You can:你可以:

  • Create a function that:创建一个函数:
    • Filters out already sent messages过滤掉已经发送的消息
    • Find messages that have a target send time on the past查找target send time在过去的消息
    • Sends those messages发送那些消息
  • Create a time-based trigger to run that function.创建一个基于时间的触发器来运行该函数。

Keep in mind that, on the best case scenario, your message will be sent at the target time, on the worst case, it will be sent on the <interval between trigger runs> time.请记住,在最好的情况下,您的消息将在目标时间发送,在最坏的情况下,它将在<interval between trigger runs>时间发送。

Also, you will need to track what has already been sent (with an extra column, or removing the rows, for example)此外,您需要跟踪已发送的内容(例如,使用额外的列或删除行)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Google Apps 电子表格脚本中将日期和时间合并为日期时间? - How to merge date and time as a datetime in Google Apps Spreadsheet script? 根据variabel Google Apps脚本日历/电子表格中的值执行功能 - Do a function based on a value in a variabel Google Apps script calendar/spreadsheet 我的 Google Apps 脚本基于时间的触发器拒绝工作,即使它没有显示任何错误 - My Google Apps Script time-based trigger refuses to work, even though it displays no errors 以编程方式将基于时间的触发器添加到Google Apps脚本中的加载项 - Programmatically adding a time-based trigger to an add-on in Google Apps Script Google Script Time-Based Trigger -&gt; 一直请求授权 - Google Script Time-Based Trigger -> Asking for Authorization all the time Google Apps脚本自定义每小时触发器在给定时间启动 - Google apps script custom hourly trigger to start at given time 使用Google Script和电子表格创建时间触发器 - Create time trigger with Google Script and spreadsheet Google Apps 脚本(电子表格) - 根据单元格中的条件选择电子表格中的数组 - Google Apps Script (Spreadsheet) - selecting an array in spreadsheet based on a condition in cells Google Apps 脚本未按时调用 function - Google Apps Script not calling function on time based Condition Google表格应用脚本超时和基于时间的触发器未运行 - Google Sheets App Script timing out and time based trigger not running
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM