English 中文(简体)
使用 MailApp. sendEmail 时正确加分
原标题:Proper Incrementing while using MailApp.sendEmail

我对谷歌的剧本场景还有些新奇。我看过很多次了,我脑筋都无法正常地在增量周围包扎。

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

到目前为止,我可以完美地填入缩放表,但是当函数运行时,它只在第1行中标出状态变量,而不在列表中继续,然后它发电子邮件给第1行,多次。有人看到我在这上面哪里出错吗?

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

S 这里的脚本 :

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 = "[email protected]";
    var subject = "ACCT:" + acct + " " + cname + " - Equipment Request";

    // setup content of e-mail
    var body = "Hello Accounting,

" +
        "Please bill for the contained items on the following account:

" +
        "Account: " + acct + "

" +
        "Client Name: " + cname + "

" +
        "Item 1: " + item1 + "

" +
        "Item 2: " + item2 + "

" +
        "Item 3: " + item3 + "

" +
        "Item 4: " + item4 + "

" +
        "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();
    };

  };
}

谢谢你抽出时间来!

最佳回答

So as mentioned above, I had to keep going over and over this. Finally, through learning a different technique and finding a few spread out threads, I ended up with the following script that: - simply checks the last row of data at time of submission - Has the user submit a form - Emails intended parties - Updates spreadsheet - Watches for breaks - Provides confirmation of email / breaks

我希望这能帮助其他人。到目前为止,我仍在研究一个可以扫描整个电子表格的脚本,但这个脚本检查了提交表格的最后一行,以回答我的特殊问题。当我找到完整表格扫描的答案时,我会尽量公布答案:

(PS: 可能更顺畅地通过 Google Scripter 将它写入 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 = "[email protected]"; // email that data needs to go too
  var sub = "ACCT: " +acct+ " - " +cname+ "Request"; // subject in email
  var body = "Hello Accounting,

"
      + "Please order the following:

"
      + "Item 1: " +it1
      + "
Item 2: " +it2
      + "
Item 3: " +it3
      + "
Item 4: " +it4
      + "

Please send us the invoicing once complete."
      + "

Submitted by
"
      + 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!");
    }
  }
}
问题回答

我在看着

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

这应该是(观察双引号)

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

它只在第一行中标出状态变量,而不在列表中继续,

你只影响第一行的原因是第三点:

var dataRange = 工作表.getRange( 工作表, 1, 1, < genger_ em > 1 , cols > );

其中应指定数据集中的行数,而您可以看到的是1(1)(1)(一),在数据框中只产生一行。请尝试以下内容,看看是否有帮助:

// 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();

然后就发了好几次一行的电子邮件

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





相关问题
How to use year counter google app script?

I need an urgent help i have data as follows in a google sheet i need to add a year counter to the column d as follows using google apps script

What is the daily email limit in Google Apps Script?

Can someone tell me if there is a webpage that lists the official Google limit (Quotas) on emails sent from a Google Apps Script? In testing my little script I got an error: Service invoked too many ...

Reference Error for property of Javascript Object

I have built an object in Javascript on the Google Apps Script engine and every time I run my script I get a reference error saying uName is not defined. Here is the relivant code: function DataSet()...

Run Google Apps Script on Google-Spreadsheet event?

If I create a Google apps script, can I hook it up to Google spreadsheet to run based on an event, or must I manually invoke it? I don t know if Google Sheets supports any events. For example: a ...

Google gadget/spreadsheet: Grab spreadsheet key for gadget

So I m working on a google gadget(really only gadgetizing so I can get a datastore for this) that I want to be embeddable in a google spreadsheet (after this point, I m going to skip the word google - ...