-1

我对谷歌脚本场景有点陌生。我已经经历了很多次,我无法正确地增加我的大脑。

我创建了一个使用表单的 Google 电子表格。当有人使用表单提交设备时,它会使用发送电子邮件功能(MailApp.sendEmail)将其通过电子邮件发送给会计人员。

到目前为止,我可以完美地填充电子表格,但是当函数运行时,它只标记第一行中的“状态”变量并且不会继续通过列表,然后它通过电子邮件多次发送该 1 行。谁能看到我在这方面哪里错了?

我需要它来将数据提交到可用的下一行,然后脚本检查最后一个单元格的 EMAIL_SENT,如果它没有那个值,我需要它发送一封电子邮件,然后检查下一行数据以及(如果我需要从电子表格中手动运行脚本)。我在这里找不到类似的问题,我可以理解我在递增和循环方面做错了什么。

这是脚本:

function sendThisOut() {
  // set the current spreadsheet and active sheet
  var sheet = SpreadsheetApp.getActiveSheet();

  // set range data
  var sRow = 2;  // first top left cell of data
  var cols = 9; // Num of cols 

  // define initial data source and get values
  var dataRange = sheet.getRange(sRow,1,1,cols);
  var data = dataRange.getValues();

  // increment data
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];

    // define each column
    var tstamp = row[0]; // get timestamp (col A)
    var acct = row[1]; // define account (col B)
    var cname = row[2]; // define client name (col C)
    var comment = row[3]; // define comments (col D)
    var item1 = row[4]; // defines item 1 (col E)
    var item2 = row[5]; // defines item 2 (col F)
    var item3 = row[6]; // defines item 3 (col G)
    var item4 = row[7]; // defines item 4 (col H)
    var status = row[8]; // get status (emailed or no?)

    // setup e-mail vars
    var emailAddr = "myEmail@myDomain.com";
    var subject = "ACCT:" + acct + " " + cname + " - Equipment Request";

    // setup content of e-mail
    var body = "Hello Accounting,\n\n" +
        "Please bill for the contained items on the following account:\n\n" +
        "Account: " + acct + "\n\n" +
        "Client Name: " + cname + "\n\n" +
        "Item 1: " + item1 + "\n\n" +
        "Item 2: " + item2 + "\n\n" +
        "Item 3: " + item3 + "\n\n" +
        "Item 4: " + item4 + "\n\n" +
        "Reason for rekey: " + comment;

    // check status and if it doesn't have EMAIL_SENT, email it
    if (status != "EMAIL_SENT") {
      MailApp.sendEmail(emailAddr, subject, body);
      // Make sure the cell is updated right away in case the script is interrupted
      sheet.getRange(sRow + i, 9).setValue(EMAIL_SENT);
      SpreadsheetApp.flush();
    };

  };
}

谢谢你的时间!

4

3 回答 3

1

它只标记第一行中的“状态”变量,并且不会继续通过列表,

你只影响第一行的原因是第三个参数:

var dataRange = sheet.getRange(sRow,1, 1 ,cols);

它应该指定数据集中的行数,如您所见,它是 1(一),这导致 dataRange 中只有一行。尝试以下方法,看看是否有帮助:

// set range data
  var sRow = 2;  // first top left cell of data
  var cols = 9; // Num of cols 
  var rows = sheet.getLastRow() - sRow + 1
  // define initial data source and get values
  var dataRange = sheet.getRange(sRow,1,rows,cols);
  var data = dataRange.getValues();

然后它通过电子邮件多次发送该 1 行。

除非在测试期间您可能设置了多个触发器,否则不确定这一点?在上述更改后尝试,如果仍然有问题,请回帖。

于 2012-05-29T15:55:36.043 回答
1

我在看

 sheet.getRange(sRow + i, 9).setValue(EMAIL_SENT);

这不应该是(观察双引号)

  sheet.getRange(sRow + i, 9).setValue("EMAIL_SENT");
于 2012-05-29T05:26:29.927 回答
0

所以如上所述,我不得不一遍又一遍地重复这个。最后,通过学习一种不同的技术并找到一些分散的线程,我最终得到了以下脚本: - 只需在提交时检查最后一行数据 - 用户是否提交了表单 - 向目标方发送电子邮件 - 更新电子表格- 观察休息 - 提供电子邮件/休息确认

我希望这对其他人有帮助。到目前为止,我仍在研究一个将扫描整个电子表格的脚本,但该脚本会检查提交表单的最后一行,这回答了我的特定问题。当我找到完整版扫描的答案时,我会尝试发布答案:

(PS:将其传递到 Google Scripter 可能会更顺畅。我添加了很多评论,以使自己和阅读此内容的任何人都了解正在发生的事情)

function other() {
  // set the current sheet
  var sheet = SpreadsheetApp.getActiveSheet();

  // set the last row and column
  var lrow = sheet.getLastRow();
  var lcol = sheet.getLastColumn();

  // set the major row range and data values
  var rowRng = sheet.getRange(lrow, 1, 1, lcol);
  var rowData = rowRng.getValues()[0];

  // setup redundancy checks for status and email
  // data status, email status and submitter are added to column headers AFTER form creation
  var statrng = sheet.getRange(lrow, 8); // set range of status
  var stat = statrng.getValue(); // get value of status in col h
  var emsrng = sheet.getRange(lrow, 9); // set range of emailed status
  var ems = emsrng.getValue(); // get value of emailed status in col i

  // get current submitters data and set ranges for it, then apply data to sheet
  var suberng = sheet.getRange(lrow, 10); // set range of submitters email
  var sube = Session.getActiveUser().getEmail(); // get submitters email
  suberng.setValue(sube); // set subs email to sheet in col j

  // setup the per cell data
  var tstamp = rowData[0]; // col a: set timestamp (created when forms created)
  var cname = rowData[1]; // col b: set the client name
  var acct = rowData[2]; // col c: set the account number cell
  var it1 = rowData[3]; // col d: set item 1
  var it2 = rowData[4]; // col e: set item 2
  var it3 = rowData[5]; // col f: set item 3
  var it4 = rowData[6]; // col g: set item 4

  // setup email vars
  var em = "myEmail@myDomain.com"; // email that data needs to go too
  var sub = "ACCT: " +acct+ " - " +cname+ "Request"; // subject in email
  var body = "Hello Accounting,\n\n"
      + "Please order the following:\n\n"
      + "Item 1: " +it1
      + "\nItem 2: " +it2
      + "\nItem 3: " +it3
      + "\nItem 4: " +it4
      + "\n\nPlease send us the invoicing once complete."
      + "\n\nSubmitted by\n"
      + sube + " : " + tstamp;

  // Now that all variables have been set, run checks and send the email as well
  // as a confirmation email. If script doesn't complete, a failure notice is sent if possible

  // as long as there's a timestamp and submitted status is empty, run if statement
  if (tstamp !== "" && stat == "") {
    statrng.setValue("Submitted"); // update status field since we've gotten this far
    var statu = statrng.getValue(); // make sure update took for status
    if (statu == "Submitted") {
      if (ems == "") { // no need to continue if email has already been sent, so check
        MailApp.sendEmail(em, sub, body); // send to accounting people
        emsrng.setValue("Emailed"); // update status that email was successfully sent
        // let submitter know it was a success
        MailApp.sendEmail(sube,"ACCT: " +acct+ " - Submission Successful","Your Submission was Successful!");
      }
    } else {
      // if form failed, let submitter know it was not a success
      MailApp.sendEmail(sube,"ACCT: " +acct+ " - Submission Successful","Your Submission was Successful!");
    }
  }
}
于 2012-06-02T19:07:41.163 回答