如果我想要更改谷歌表单中的答案,比如对MultipleChoice项目的回复,我该怎么做?我可以在提交时触发脚本的执行,但它是在推送到电子表格之前还是之后发生的?
以下代码在提交时触发,提交也会导致表单数据被推送到链接的电子表格中。电子表格会更新,但结果不是我想要的结果。
function setPrice() {
var ws = SpreadsheetApp.openByUrl("spreadsheet_link");
var ss = ws.getSheetByName("BPS")
var inventory = ss.getRange("A2:A280").getValues();
var prices = ss.getRange("B2:B280").getValues();
var prices_no_bp = ss.getRange("C2:C280").getValues();
var form = FormApp.getActiveForm();
var formItems = form.getItems();
var item_selected = form.getItemById("Question 1").asListItem();
var bp_selected = form.getItemById("Question 2").asListItem();
var price_selected = form.getItemByID("Price").asMultipleChoiceItem;
var formResponse = form.createResponse();
// scan the inventory list, find the item selected, and return the price
var this_price = 999999999;
var sl = 8;
for (var i = 0;i < sl; i++) {
if (inventory[i] == item_selected) {
if (bp_selected == "Yes") {
this_price = prices_no_bp[i];
} else {
this_price = prices[i];
}
}
}
// Create the multiple choice entry
// Change the response to the same value
// Attach the response to the form
price_selected.createChoice(this_price);
var response = price_selected.createResponse(this_price);
formResponse.withItemResponse(response)
// Responses are ready for submission
}这段代码不能工作是因为我在推送到电子表格之后更改了响应,还是因为我按正确的顺序进行了更改,但并没有真正更改响应?
发布于 2021-10-06 15:51:33
在触发的代码运行之前,会将响应推入响应电子表格。因此,通过onSubmit触发器以编程方式更改响应是没有意义的。最好的选择是使用响应电子表格,因为那里有可用的数据。
https://stackoverflow.com/questions/69456181
复制相似问题