【发布时间】:2016-02-20 12:53:28
【问题描述】:
我为谷歌创建了一个在提交表单上运行的脚本。 它需要一个在表单中输入的数字,然后在工作表中查找它。 如果找到匹配项,则会在提交表单中输入时间戳。
在这里
// this is the name of our script, myFunction can be anything.
// e is a special variable, its the form & answers that has
// just been submitted by a user.
function myFunction(e) {
var sessionDate = e.values[0]; // This gets the timestamp of the submission
var sessionID = e.values[1]; // This gets the session ID the user entered
var ss = SpreadsheetApp.getActiveSpreadsheet(); // Gets the spreadsheet where the form is attached to and all sheets are stored
var sheet = ss.getSheetByName("Data Dump"); // Specifies the data dump sheet, which has the IDs and the cell we want to add the timestamp to
var range = sheet.getRange("B:B"); // Specifies the column that holds the session IDs, in the data dump sheet
var values = range.getValues(); // Reads the data in the sessions ID column, that we specified earlier
for(i=0;i<values.length;i++){ // This goes through the session ids one by one, and we can test each one
if (values[i][0] == sessionID){ // This is the test. If the ID we are currently looking at matches the ID on the form response, then lets do something
sheet.getRange(i+1, 7).setValue(sessionDate); // This is us doing that something. Using where we found the matching ID, lets write the timestamp in a cell.
break; // This stops us going through anymore ID's as we have already found the one we are looking for
}
}
}
经过反复测试,效果很好。然后它被设置为上线。 与很多人共享表单。
有很多提交,然后脚本停止工作。即使在手动测试时,我也没有收到任何具体错误。只是电子表格服务错误。
所以我在一两天前就停止了对表单的回复。今天是我必须查看的第一个更改,并且一切正常。脚本在几秒钟内运行。
我唯一能想到的是脚本对于谷歌来说太密集了,需要多次提交,我特别想for 循环可能会给它带来问题。
有没有人有任何想法或任何我可以尝试的?
提前致谢
【问题讨论】:
-
可能是用户提交了无效的 sessionID,数据转储表的行数过多。
-
有人能解释一下否决票吗?在之前的问题反馈之后。我已经确保我注释了我的代码,并试图很好地布局它。 (感谢您的编辑)。试图确保问题和解释简明扼要。因此,如果您有任何其他反馈来提高未来的问题质量,我们将不胜感激。
-
@Rubén 无效的会话 ID 很重要。因为我们只在匹配成功时才做事。如果没有匹配,则什么也不做。我已经提交了 ID 不匹配的回复,但它仍在运行。我将研究太多行的想法。谢谢
-
我怀疑反对意见来自 JavaScript 论坛,当他们看到不是“纯”编码问题的帖子时,那里的一些人有点咄咄逼人......但我没有足够的声誉看到那种信息,所以这只是基于经验的怀疑;-)
-
实际上,您不需要它来格式化您的帖子(您可以随时删除)。顺便说一句,我赞成补偿(几乎;-))
标签: google-apps-script google-sheets google-forms