如何传递正确的参数

时间:2014-07-22 15:43:53

标签: google-apps-script

我有此错误消息:     找不到方法getRange((class),number)。 (第37行,文件“回复”)

// Create a time-driven trigger
//function createTimeTrigger() {
// trigger every 6 hours
//var everyXminutes = ScriptApp.newTrigger("Main")
  //.timeBased()
  //.everyMinutes(15)
  //.create();
//}

function Main() {
  var row = EmailFormConfirmation( 2 );
  row = EmailFormConfirmation( row );
}

function EmailFormConfirmation( number ){

  var row = number;
  var col = 2  // column to find email addresses

  // Main script
  var ss = SpreadsheetApp.openById("1ssxFwNRi7r4G5cbhlNYXj5Fm9XhbB0B242Pen3ZTmE4");
  var sheet = ss.getActiveSheet();
  var email = sheet.getRange(number, col).getValue();

   while ( email != "" ) {
  var name = sheet.getRange(row,3).getValue() + " " + sheet.getRange(row,4).getValue(); // combines fields for first and last names
    var subject = "PROJECT: REPLY Thank you for RSVPing to " + sheet.getSheetName() + "!"
    var message = "Hello " + name+ ","+'\n'+'\n'+
      subject + '\n';
    MailApp.sendEmail( email, subject, message );

    row += 1;
    return row;
  }
}

似乎问题是'row'是作为一个类传递的?我不确定发生了什么。但是我需要能够在Main()中增加,因为它是根据EmailFormConfirmation的运行方式在时间驱动的触发器上调用的。

2 个答案:

答案 0 :(得分:0)

这不是Apps脚本问题,而是一个糟糕的程序构建......

  • 你的while循环将永远不会工作,因为你在循环中包含一个强制退出函数的返回
  • 如果您移动返回,它将永远不会结束,因为电子邮件值未在循环中更新>>无限循环
  • row += 1不一定会将行增加为整数(可能会变为row1),而是使用row++而不是
  • 我真的不明白你的第二行主要目的是row = EmailFormConfirmation( row );,因为第一次调用EmailFormConfirmation已经在循环中递增了......

下面是一个工作代码(不确定它是否符合您的要求):

function Main() {
  var row = EmailFormConfirmation( 2 );
  Logger.log('row final value='+row);
}

function EmailFormConfirmation(row){

  var col = 2  // column to find email addresses

  // Main script
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var email = sheet.getRange(row, col).getValue();

  while ( email != "" ) {
    var email = sheet.getRange(row, col).getValue();
    var name = sheet.getRange(row,3).getValue() + " " + sheet.getRange(row,4).getValue(); // combines fields for first and last names
    var subject = "PROJECT: REPLY Thank you for RSVPing to " + sheet.getSheetName() + "!"
    var message = "Hello " + name+ ","+'\n'+'\n'+
      subject + '\n';
//    MailApp.sendEmail( email, subject, message );    
    row++;
    Logger.log(row);
  }
  return row;
}

答案 1 :(得分:0)

以下代码现在可以正常使用。

function send(row, sheet, email){
  var name = sheet.getRange(row,3).getValue() + " " + sheet.getRange(row,4).getValue(); // combines fields for first and last names
  var subject = "PROJECT: REPLY Thank you for RSVPing to " + sheet.getSheetName() + "!" // sheet should be manually named based on 
  //var url = 
  var body = "Hello " + name+ ","+'\n'+'\n'+
      subject + '\n';
  MailApp.sendEmail( email, subject, body ); // or { htmlbody: ""}
}

function Main() {
  var ss = SpreadsheetApp.openById("1ssxFwNRi7r4G5cbhlNYXj5Fm9XhbB0B242Pen3ZTmE4");  // Id indicated in url
  var sheet = ss.getActiveSheet();
  if (sheet.getRange(2,9).getValue() == "") {
    var row = 2;
  }
  else {
    var row = sheet.getRange(2,9).getValue() + 2;
  }
  sheet.getRange(1,9).setValue("Emails sent");
  while ( sheet.getRange(row,2).getValue() != "" ) {
    send(row, sheet, sheet.getRange(row,2).getValue());
    row += 1;
    sheet.getRange(2,9).setValue( row - 2 );
  }
}

这将保存运行该功能所需的数据,而无需重新发送到任何电子邮件地址。