onEdit 脚本不起作用我已经更改了几次才能找到问题

问题描述 投票:0回答:1

我收到一个错误,它无法找到源。我更改了脚本并修复了该问题,但现在出现错误( TypeError: Cannot readproperties of undefined (reading 'range') 我究竟做错了什么? 这是我的脚本:

"function onEdit(e) {

  addTimeStamp(e);
  senEmail(e);
  
}


function addTimeStamp(e){
  var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet5") ;
  var range = e.range;
  var row = range.getRow();
  var col = range.getColumn();
  var cellValue = range.getValue();


  if (col == 9 && cellValue === true){
    sheet.getRange(row,10).setValue(new Date());
  }
}


function senEmail(e){
  var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet5") ;
  var range = SpreadsheetApp.getActiveSpreadsheet().getRangeByName('Sheet5');
  var row = range.getRow();
  var col = range.getColumn();
  let cellValue = range.getValue(); // The value of the edited cell

  // Check if the edit occurred in column 5 and the value is true
  if (col == 9 && cellValue === true) {
  
  let email = sheet.getRange(row, 2).getValue();
  let name = sheet.getRange(row, 2);
  let orderID = sheet.getRange(row, 5);
  let orderDate =sheet.getRange(row, 1);
  let methodOfPayment = sheet.getRagne(row, 4);
  let numberOfTickets = sheet.getRagne(row, 6);
  let payableTotal = sheet.getRange(row, 7);
  let html_link ="https://checkout.payableplugins.com/order/"+[row,5];


  const subject = "PHLL Raffle Fundraiser Follow-Up on Order ID #" +row[5];
  const messageBody = 'Dear' + name + ',' + "\n\n" + 'We hope this message finds you well. We wanted to remind you that we have received your order information for the raffle ticket. According to our records, you have selected the cash method for payment, but we have not yet received the payment.' + "\n\n" + 'If you have already provided the payment to the player, please let us know as soon as possible. This will allow us to coordinate with them to ensure that your payment is collected and your raffle ticket is processed accordingly.' + "\n\n" + 'However, if you have not yet provided the payment to the player, we kindly ask you to do so at your earliest convenience. Once the payment is received, we will promptly send you your raffle ticket.' + "\n\n" +
'Please note that if payment is not received within the specified timeframe, we will have to remove your name from the drawing.' + "\n\n" +  'If you wish to change your method of payment, you may do so by following this link:' + html_link +   + "\n\n" + 'Should you have any further questions or concerns, please do not hesitate to reach out to us. We are here to assist you in any way we can.' + "\n\n" +  'Best of luck in the raffle drawing!'  + "\n\n" + 'Warm regards,'
const messageBody2 = 'ORDER DETAILS:' + "\n\n" + 'ORDER DATE:' + orderDate +"\n\n" + 'ORDER ID #' + orderID +"\n\n" + 'METHOD OF PAYMENT:' + methodOfPayment + "\n\n" + 'NUMBER OF TICKETS:' + numberOfTickets +"\n\n" +'PAYABLE TOTAL:' + payableTotal
const respondent = 'Treasurer' + "\n\n" + 'EmailAddress'   + "\n\n" + 'Paradise Hills Little League'

MailApp.sendEmail(email,subject,messageBody,messageBody2,respondent);

console.log(sendEmail)

}
}
google-sheets google-apps-script
1个回答
0
投票

在这里发现了一些问题:首先,第25行中的范围调用的是工作表,而不是单元格范围。我已将其更改为

e.range
以调用事件对象。

此外,我相信您必须使用可安装的 onEdit 触发器才能使用邮件服务。我将您的函数名称从

onEdit
更改为
onMyEdit
并设置了 onEdit 触发器。

其次还有一些拼写错误:

首先,第 4 行和第 23 行中的

senEmail()
。如果您不调用该函数但在代码的最后一行调用,也没关系。

第二个,第 37 行和第 38 行中的

getRagne()

此外,进行这些更正后,我收到以下错误:

异常:参数 (String,String,String,String,String) 不 与 MailApp.sendEmail 的方法签名匹配。

我组合了messageBody、messageBody2和respondent,使其与方法签名保持一致。

最后,您可以在同一个手机上拨打姓名和电子邮件。我将名称更改为第 3 列,但如果此假设不正确,则可以轻松修改。

这些更改反映在下面的代码中:

function onMyEdit(e) {

  addTimeStamp(e);
  sendEmail(e);
  
}


function addTimeStamp(e){
  var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet5") ;
  var range = e.range;
  var row = range.getRow();
  var col = range.getColumn();
  var cellValue = range.getValue();


  if (col == 9 && cellValue === true){
    sheet.getRange(row,10).setValue(new Date());
  }
}


function sendEmail(e){
  var sheet = SpreadsheetApp.getActive().getSheetByName("Sheet5") ;
  var range = e.range;
  var row = range.getRow();
  var col = range.getColumn();
  let cellValue = range.getValue(); // The value of the edited cell

  // Check if the edit occurred in column 5 and the value is true
  if (col == 9 && cellValue === true) {
  
  let email = sheet.getRange(row, 2).getValue();
  let name = sheet.getRange(row, 3).getValue();
  let orderID = sheet.getRange(row, 5).getValue();
  let orderDate =sheet.getRange(row, 1).getValue();
  let methodOfPayment = sheet.getRange(row, 4).getValue();
  let numberOfTickets = sheet.getRange(row, 6).getValue();
  let payableTotal = sheet.getRange(row, 7).getValue();
  let html_link ="https://checkout.payableplugins.com/order/"+orderID;


  const subject = "PHLL Raffle Fundraiser Follow-Up on Order ID #" + orderID ;
  const messageBody = 'Dear ' + name + ',' + "\n\n" + 'We hope this message finds you well. We wanted to remind you that we have received your order information for the raffle ticket. According to our records, you have selected the cash method for payment, but we have not yet received the payment.' + "\n\n" + 'If you have already provided the payment to the player, please let us know as soon as possible. This will allow us to coordinate with them to ensure that your payment is collected and your raffle ticket is processed accordingly.' + "\n\n" + 'However, if you have not yet provided the payment to the player, we kindly ask you to do so at your earliest convenience. Once the payment is received, we will promptly send you your raffle ticket.' + "\n\n" +
'Please note that if payment is not received within the specified timeframe, we will have to remove your name from the drawing.' + "\n\n" +  'If you wish to change your method of payment, you may do so by following this link: empty.  Should you have any further questions or concerns, please do not hesitate to reach out to us. We are here to assist you in any way we can.' + "\n\n" +  'Best of luck in the raffle drawing!'  + "\n\n" + 'Warm regards,' + "\n\n" + 'Treasurer' + "\n\n" + 'EmailAddress'   + "\n\n" + 'Paradise Hills Little League' + "\n\n" + 'ORDER DETAILS:' + "\n\n" + 'ORDER DATE:' + orderDate +"\n\n" + 'ORDER ID #' + orderID +"\n\n" + 'METHOD OF PAYMENT:' + methodOfPayment + "\n\n" + 'NUMBER OF TICKETS:' + numberOfTickets +"\n\n" +'PAYABLE TOTAL:' + payableTotal;

MailApp.sendEmail(email,subject,messageBody);

console.log(sendEmail)

}
}

注意:您无法从脚本编辑器运行此代码。需要有一个事件对象才能运行脚本。您需要从电子表格本身进行测试。

如果这不适合您,请告诉我。

© www.soinside.com 2019 - 2024. All rights reserved.