我正在尝试创建一个电子表格,该电子表格实时从站点导入数据,并且每次进行“交易”时都会向一组人员发送Google表单,供他们对交易进行投票。
为确保每个人只有一票,我将其设置为创建唯一的ID,并将对照发送的内容进行检查。
但是,我希望每次发送表单响应时都能够解析表单响应:
我的问题是,当我运行此代码时,似乎从未触发过onFormSubmit(e)。我将代码另存为我的项目的“ forms.gs”。我脚本底部的垃圾只是为了尝试查看是否触发了onFormSubmit。调用触发器时我做错了什么吗?另外,是否有更特定的方法来调用此触发器(以防我想包括其他触发器,例如时间或电子表格的onedit)?谢谢!
这是我的代码:
function updatecomparelist() {
var spreadsheet = SpreadsheetApp.getActive();
var questionsheet = spreadsheet.getSheetByName("Question list");
var newtradesheet = spreadsheet.getSheetByName("Trades Cut");
var newnameandtime = newtradesheet.getRange('D1');
var isjustaveto = newtradesheet.getRange('C1');
var comparesheet = spreadsheet.getSheetByName("Compare");
var oldnameandtime = comparesheet.getRange('D1');
if (newnameandtime.getValue() != oldnameandtime.getValue() && isjustaveto.getValue() != "Vetoed Trade to"){
comparesheet.getRange('1:1').activate();
comparesheet.insertRowsBefore(comparesheet.getActiveRange().getRow(), 2);
newtradesheet.getRange('A1:D2').copyTo(comparesheet.getRange('A1:D2'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
var tradeTime = new Date(newtradesheet.getRange('P1').getValue());
var theTrade = questionsheet.getRange('B2').getValue();
createNewForm(spreadsheet,theTrade,tradeTime);
}
};
function createNewForm(spreadsheet,theTrade,tradeTime) {
var now = new Date();
var emailList = spreadsheet.getSheetByName("Email List").getRange('A1:A2');
var emailListCut = spreadsheet.getSheetByName("Email List").getRange('B1:B2');
var emailListCutValues = emailListCut.getValues();
var uniqueIDList = [];
var expression = "";
for (var i = 0; i<3; i++){
var randnumber = Math.floor((Math.random() * 99998) + 1);
uniqueIDList[i] = emailListCutValues[i] + randnumber;
spreadsheet.getSheetByName("Email List").getRange('C'+i+1).setValue(uniqueIDList[i]);
var expression = expression + uniqueIDList[i] + "|";
spreadsheet.getSheetByName("Email List").getRange('C'+i+2).setValue(expression);
}
var form = FormApp.create('Trade Veto Survey');
form.setDestination(FormApp.DestinationType.SPREADSHEET, spreadsheet.getId());
form.setDescription(theTrade);
var question = form.addMultipleChoiceItem();
question.setTitle('How do you vote?')
.setChoices([
question.createChoice('Accept'),
question.createChoice('Veto')
]);
question.setRequired(true);
var item = form.addTextItem();
item.setTitle("Unique user ID (DO NOT CHANGE):");
item.setRequired(true);
var textValidation = FormApp.createTextValidation()
.setHelpText('Your user ID is incorrect.')
.requireTextMatchesPattern(expression)
.build();
item.setValidation(textValidation);
form.setAllowResponseEdits(true);
form.setPublishingSummary(true);
ScriptApp.newTrigger('onFormSubmit')
.forSpreadsheet(spreadsheet)
.onFormSubmit()
.create();
}
function onFormSubmit(e) {
var formResponses = {vote: e.namedValues['How do you vote?'], names: e.namedValues['Unique user ID (DO NOT CHANGE):']};
var sheetActive = SpreadsheetApp.openById("1995364161");
var sheet = sheetActive.getSheetByName("Email List");
var dateSheet = SpreadsheetApp.openByID('1928596830');
var tradeTime = new Date(dateSheet.getRange('P1').getValue());
dateSheet.getRange('Z1').setValue(tradeTime);
var now = new Date();
var form = FormApp.activeForm();
if (now.valueOf() > tradeTime.valueOf()){
form.setAcceptingResponses(false);
form.setCustomClosedFormMessage("Sorry, the 24 hour veto window has passed.");
}
sheet.getRange('F1').setValue(formResponses);
Logger.log(formResponses);
var formResponses2 = form.getResponses();
for (var i = 0; i < formResponses.length; i++) {
var formResponse = formResponses[i];
var itemResponses = formResponse.getItemResponses();
for (var j = 0; j < itemResponses.length; j++) {
var itemResponse = itemResponses[j];
sheet.getRange('D'+j).setValue(itemResponse[j]);
sheet.getRange('E'+i).setValue(formResponse[i]);
Logger.log('Response #%s to the question "%s" was "%s"',
(i + 1).toString(),
itemResponse.getItem().getTitle(),
itemResponse.getResponse());
}
}
}