简体   繁体   English

无法让我在表格上的 Google Apps 脚本自动触发

[英]Can't get my Google Apps script on Sheets to trigger automatically

I made myself a code for Google Sheets that sends me an automatic email whenever a condition is met.我为自己编写了 Google Sheets 的代码,只要满足条件,它就会自动向我发送电子邮件。 I have managed to make this work, but only when I manually edit the sheet - I need it to trigger automatically when the sheet changes itself.我已经设法完成了这项工作,但仅当我手动编辑工作表时 - 我需要它在工作表自身更改时自动触发。

My Sheet manages my subscriptions for my clients, whenever the cell on the first column goes to the number 3 - that row is marked as "impending" - and ideally sends me the email.我的工作表为我的客户管理我的订阅,只要第一列上的单元格转到数字 3 - 该行被标记为“即将发生” - 并理想地向我发送电子邮件。 (The number 3 automatically appears whenever the expiry date of that item is under 2 weeks). (只要该项目的有效期不到 2 周,数字 3 就会自动出现)。

Please let me know if you think you can spot where I haven't set thing up properly :) Thanks!如果您认为您可以发现我没有正确设置的地方,请告诉我:) 谢谢!

In Google Apps Script I've tried setting the trigger (but can't get it to be automatic)在 Google Apps Script 中,我尝试设置触发器(但无法自动设置)

Choose which function to run
sendMailEdit

Which runs at deployment
Head

Select event source
From spreadsheet

Select event type
On edit (also tried On change)

And here's my code:这是我的代码:

    function sendMailEdit(e){
  if (e.range.columnStart != 1 || e.value !=3) return;
  const rData = e.source.getSheetByName('SUBSCRIPTIONS').getRange(e.range.rowStart,1,1,14).getValues();

  let mis = rData[0][1];
  let dat = new Date(rData[0][2]).toLocaleDateString("en-US");
  let cos = rData[0][3];
  let aut = rData[0][4];
  let typ = rData[0][5];
  let des = rData[0][6];
  let pro = rData[0][7];
  let not = rData[0][8];
  let cnu = rData[0][9];
  let cna = rData[0][10];
  let sta = rData[0][13];
  let now = new Date().toLocaleString("en-US");

  
  let msg = ":: AUTOMATED EMAIL FROM - NP PROJECTS SHEET 2021 / SUBSCRIPIONS PAGE :: " + "\n" + "\n" + sta + " SUBSCRIPTION FOR: " + "\n" + dat + " (" + mis + " days missing)"  + "\n" + "Cost: " + cos + "$" + "\n" + "Is autopay active: " + aut + "\n" + "\n" +"CLIENT: " + cna + " (" + cnu + ")" + "\n" + "\n" + "Product: " + typ + "\n" + "\n"+"Descprition: " + des  + "\n" + "\n" + "Provider: " + pro  + "\n" + "\n"+ "Notes: " + not  + "\n" + "\n"+ "\n" + "(This email was triggered at:" +  now + ")";
  
  Logger.log(msg);
  GmailApp.sendEmail("myemail@gmail.com", "IMPENDING SUBSCRIPTION (automail)", msg)
}

Why not do onEdit() :为什么不做onEdit()

function onEdit(e){
  const col = e.range.getColumn()
  const sheetName = e.source.getActiveSheet().getName()

  // skip if not col 1 or "My Target Sheet" is the sheet name
  if( col != 1 || sheetName != "My Target Sheet") return

  // here you can get the data and send the Email
  // you may need to call a function sendEmail(){} as I am not sure whether you can trigger an email send in the onEdit(e) function

  
}

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

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM