0

我一直在寻找没有任何运气的答案,所以我希望比我更聪明的人以前见过这个。这是我的问题:

I. 我有一个绑定到电子表格的 Google 表单来捕获订单数据。该电子表格在一个单元格中有一个数组公式,用于根据表单 (SubTotal) 中的选择计算美元金额。

二、我创建了一个 GAS 以根据表单数据向订购方发送确认电子邮件并复制其他相关方。它有一个 OnFormSubmit 的触发事件。

三、一切都很好,除了字段 SubTotal 的值在脚本生成的电子邮件中显示为“未定义”。

这是 GAS 代码:

function orderconfemail(e) {
  var timestamp = e.values[0];
  var playerLastName = e.values[1];
  var playerFirstName = e.values[2];
  var jerseyNumber = e.values[3];
  var ageGroup = e.values[5];
  var parentLastName = e.values[6];
  var parentFirstName = e.values[7];
  var toAddress = e.values[8];
  var ccAddress = e.values[17];
  var teamName = e.values[13];
  var coachName = e.values[14];
  var mgrName = e.values[16];
  var homeSize = e.values[20];
  var homeQty = e.values[21];
  var awayOrdered = e.values[22];
  var awaySize = e.values[23];
  var awayQty = e.values[24];
  var shortSize = e.values[26];
  var shortQty = e.values[27];
  var jcktOrdered = e.values[28];
  var jcktSize = e.values[29];
  var jcktQty = e.values[30];
  var pntsOrdered = e.values[31];
  var pntsSize = e.values[32];
  var pntsQty = e.values[33];
  var bagOrdered = e.values[34];
  var bagQty = e.values[35];
  var orderTotal = e.values[36];
  var subject = "Futura FC Uniform Order Confirmation for " + playerFirstName + " " + playerLastName;
  var htmlBody = "Thank you for your uniform order, " + parentFirstName + "! Your order date / time was: " + timestamp + "." + 
      "<br/>&nbsp;</br>Your order details are as follows: " +
      "<br/>&nbsp;<br/><b>Player Name:</b> " + playerFirstName + " " + playerLastName +
      "<br/><b>Jersey Number:</b> " + jerseyNumber +
      "<br/><b>Age Goup:</b> " + ageGroup +
      "<br/><b>Team:</b> " + teamName +
      "<br/><b>Coach:</b> " + coachName +
      "<br/><b>Manager:</b> " + mgrName +
      "<br/><b>Home Jersey Size:</b> " + homeSize + 
      "<br/><b>Home Jersey Qty:</b> " + homeQty +
      "<br/><b>Away Jersey Ordered?</b> " + awayOrdered +
      "<br/><b>Away Jersey Size:</b> " + awaySize +
      "<br/><b>Away Jersey Qty:</b> " + awayQty +
      "<br/><b>Short Size:</b> " + shortSize +
      "<br/><b>Short Qty:</b> " + shortQty +
      "<br/><b>Warm Up Jacket Ordered?</b> " + jcktOrdered +
      "<br/><b>Jacket Size:</b> " + jcktSize +
      "<br/><b>Jacket Qty:</b> " + jcktQty +
      "<br/><b>Warm Up Pants Ordered?</b> " + pntsOrdered +
      "<br/><b>Pants Size:</b> " + pntsSize +
      "<br/><b>Pants Qty:</b> " + pntsQty +
      "<br/><b>Backpack Ordered?</b> " + bagOrdered +
      "<br/><b>Backpack Qty:</b> " + bagQty +
      "<br/>&nbsp;<br/><b>Your Order Total:</b> " + orderTotal;
  var optAdvancedArgs = {cc: ccAddress, bcc: "someone@aol.com,someone@embarqmail.com", htmlBody: htmlBody};
  MailApp.sendEmail(toAddress, subject, "Message Body", optAdvancedArgs);
}

这就是它在我的电子邮件中的显示方式(为了空间,我删除了除小计之外的所有内容)。

谢谢你的制服订单,家长!您的订单日期/时间是:5/23/2012 20:10:23。

您的订单总额:未定义

电子表格确实显示了正确的计算小计,但电子邮件没有。我可以吗:

I. 计算 GAS 中的小计?

二、强制脚本在发送电子邮件之前将行写入电子表格?

提前致谢!

4

3 回答 3

1

同意恩里克。最好的办法是计算 GAS 脚本中的总数。代替

var orderTotal = e.values[36];

你可以有

var orderTotal = // 在这里进行计算。但是,请确保您有 parseInt(e.values[x]),因为 e.values 中的所有参数都是字符串。

于 2012-05-24T05:16:47.970 回答
0

二、强制脚本在发送电子邮件之前将行写入电子表格?

在触发函数之前,数据已经在电子表格中(因此,无法强制执行)。

问题是您正在获取事件参数中传递的值,这些值是在表单中发送的(所有字符串),而不是电子表格上的值,如您所想。

不幸的是,识别实际表单值被保存的行(以便您可以获取它们)并不像我们想要的那么容易。请查看第 128期了解更多信息。

orderTotal无论如何,在您的情况下,我认为在脚本本身中计算要容易得多。

于 2012-05-24T03:30:33.023 回答
0

Henrique 和 Srik 都非常有助于为我指明正确的方向。我能够创建另一个变量,将每个数量解析为一个整数,然后做一些简单的数学运算来进行计算。这是我正常运行的 GAS 脚本现在的样子:

function orderconfemail(e) {
  var timestamp = e.values[0];
  var playerLastName = e.values[1];
  var playerFirstName = e.values[2];
  var jerseyNumber = e.values[3];
  var ageGroup = e.values[5];
  var parentLastName = e.values[6];
  var parentFirstName = e.values[7];
  var toAddress = e.values[8];
  var ccAddress = e.values[17];
  var teamName = e.values[13];
  var coachName = e.values[14];
  var mgrName = e.values[16];
  var homeSize = e.values[20];
  var homeQty = e.values[21];
  var homeQtyInt = parseInt(e.values[21]);
  var awayOrdered = e.values[22];
  var awaySize = e.values[23];
  var awayQty = e.values[24];
  var awayQtyInt = parseInt(e.values[24]);
  var shortSize = e.values[26];
  var shortQty = e.values[27];
  var shortQtyInt = parseInt(e.values[27]);
  var jcktOrdered = e.values[28];
  var jcktSize = e.values[29];
  var jcktQty = e.values[30];
  var jcktQtyInt = parseInt(e.values[30]);
  var pntsOrdered = e.values[31];
  var pntsSize = e.values[32];
  var pntsQty = e.values[33];
  var pntsQtyInt = parseInt(e.values[33]);
  var bagOrdered = e.values[34];
  var bagQty = e.values[35];
  var bagQtyInt = parseInt(e.values[35]);
  var orderTotal = ((homeQtyInt*40.50)+(awayQtyInt*25)+(shortQtyInt*24)+(jcktQtyInt*50)+(pntsQtyInt*32.50)+(bagQtyInt*40));
  var subject = "TEST! Futura FC Uniform Order Confirmation for " + playerFirstName + " " + playerLastName;
  var htmlBody = "Thank you for your uniform order, " + parentFirstName + "! Your order date / time was: " + timestamp + "." + 
      "<br/>&nbsp;</br>Your order details are as follows: " +
      "<br/>&nbsp;<br/><b>Player Name:</b> " + playerFirstName + " " + playerLastName +
      "<br/><b>Jersey Number:</b> " + jerseyNumber +
      "<br/><b>Age Goup:</b> " + ageGroup +
      "<br/><b>Team:</b> " + teamName +
      "<br/><b>Coach:</b> " + coachName +
      "<br/><b>Manager:</b> " + mgrName +
      "<br/><b>Home Jersey Size:</b> " + homeSize + 
      "<br/><b>Home Jersey Qty:</b> " + homeQty +
      "<br/><b>Away Jersey Ordered?</b> " + awayOrdered +
      "<br/><b>Away Jersey Size:</b> " + awaySize +
      "<br/><b>Away Jersey Qty:</b> " + awayQty +
      "<br/><b>Short Size:</b> " + shortSize +
      "<br/><b>Short Qty:</b> " + shortQty +
      "<br/><b>Warm Up Jacket Ordered?</b> " + jcktOrdered +
      "<br/><b>Jacket Size:</b> " + jcktSize +
      "<br/><b>Jacket Qty:</b> " + jcktQty +
      "<br/><b>Warm Up Pants Ordered?</b> " + pntsOrdered +
      "<br/><b>Pants Size:</b> " + pntsSize +
      "<br/><b>Pants Qty:</b> " + pntsQty +
      "<br/><b>Backpack Ordered?</b> " + bagOrdered +
      "<br/><b>Backpack Qty:</b> " + bagQty +
      "<br/>&nbsp;<br/><b>Your Order Total:</b> " + orderTotal;
  var optAdvancedArgs = {cc: ccAddress, bcc: "mmcgriggs@aol.com,futurauniforms@embarqmail.com", htmlBody: htmlBody};
  MailApp.sendEmail(toAddress, subject, "Message Body", optAdvancedArgs);
}

我也遇到了另一个“陷阱”。我的表单有一些允许 NULL 值的 Qty 字段。遇到该值时,OrderTotal 将在电子邮件中报告为“NaN”。我只是返回并将 0 作为我所有数量字段的默认值来纠正这个问题。谢谢大家!

于 2012-05-25T05:03:57.957 回答