我的脚本已绑定到Google表格。它创建一个表单并将目标设置为该工作表。然后,当我向表单提交答案时,大约有25%的答案开始正常显示在工作表中。否则,工作表中不会填充任何内容(但是当我手动取消链接并将表单重新链接到我的工作表时,它将开始正常工作。)
我在下面为脚本创建的表单之一运行了宏,并且在任何时候目标ID都一直保持在我的工作表中。
function test() {
var form = FormApp.openById('1TmyLZn5IQt62hQCQR9xOwR_Tt73MI1l8-7wM_Qq8VXQ');
var ssID = form.getDestinationId();
Logger.log(ssID);
};
我进行了研究,但找不到一个已知问题。谁能验证这个问题或帮助我找到解决方法?
谢谢!
这是我的完整代码:
var RECURRING_KEY = "recurring";
var ARGUMENTS_KEY = "arguments";
/**
* Sets up the arguments for the given trigger.
*
* @param {Trigger} trigger - The trigger for which the arguments are set up
* @param {*} functionArguments - The arguments which should be stored for the function call
* @param {boolean} recurring - Whether the trigger is recurring; if not the
* arguments and the trigger are removed once it called the function
*/
function setupTriggerArguments(trigger, functionArguments, recurring) {
var triggerUid = trigger.getUniqueId();
var triggerData = {};
triggerData[RECURRING_KEY] = recurring;
triggerData[ARGUMENTS_KEY] = functionArguments;
PropertiesService.getScriptProperties().setProperty(triggerUid, JSON.stringify(triggerData));
}
/**
* Function which should be called when a trigger runs a function. Returns the stored arguments
* and deletes the properties entry and trigger if it is not recurring.
*
* @param {string} triggerUid - The trigger id
* @return {*} - The arguments stored for this trigger
*/
function handleTriggered(triggerUid) {
var scriptProperties = PropertiesService.getScriptProperties();
var triggerData = JSON.parse(scriptProperties.getProperty(triggerUid));
if (!triggerData[RECURRING_KEY]) {
deleteTriggerByUid(triggerUid);
}
return triggerData[ARGUMENTS_KEY];
}
/**
* Deletes trigger arguments of the trigger with the given id.
*
* @param {string} triggerUid - The trigger id
*/
function deleteTriggerArguments(triggerUid) {
PropertiesService.getScriptProperties().deleteProperty(triggerUid);
}
/**
* Deletes a trigger with the given id and its arguments.
* When no project trigger with the id was found only an error is
* logged and the function continues trying to delete the arguments.
*
* @param {string} triggerUid - The trigger id
*/
function deleteTriggerByUid(triggerUid) {
if (!ScriptApp.getProjectTriggers().some(function (trigger) {
if (trigger.getUniqueId() === triggerUid) {
ScriptApp.deleteTrigger(trigger);
return true;
}
return false;
})) {
console.error("Could not find trigger with id '%s'", triggerUid);
}
deleteTriggerArguments(triggerUid);
}
/**
* Deletes a trigger and its arguments.
*
* @param {Trigger} trigger - The trigger
*/
function deleteTrigger(trigger) {
ScriptApp.deleteTrigger(trigger);
deleteTriggerArguments(trigger.getUniqueId());
}
function runFullProgram(){
var timeTriggerAll = ScriptApp.newTrigger('refresh')
.timeBased()
.everyMinutes(1)
.create();
setupTriggerArguments(timeTriggerAll, [], true);
}
function refresh() {
var spreadsheet = SpreadsheetApp.getActive();
var cellFunction = '=IFERROR(IMPORTHTML("https://basketball.fantasysports.yahoo.com/nba/190152/transactions?transactionsfilter=trade","table",2),IMPORTHTML("https://basketball.fantasysports.yahoo.com/nba/190152/transactions?transactionsfilter=trade","table",2))';
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Import Trades").getRange('A1').setValue(cellFunction);
var newtradesheet = spreadsheet.getSheetByName("Trades Cut");
var newnameandtime = newtradesheet.getRange('I1');
var isjustaveto = newtradesheet.getRange('C1');
var oldnameandtime = newtradesheet.getRange('J1');
if (newnameandtime.getValue() != oldnameandtime.getValue() && isjustaveto.getValue() != "Vetoed Trade to"){
newnameandtime.copyTo(newtradesheet.getRange('J1'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
var questionsheet = spreadsheet.getSheetByName("Question list");
var theTrade = questionsheet.getRange('B2').getValue();
createNewForm(spreadsheet,theTrade);
}
};
function createNewForm(spreadsheet,theTrade) {
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<2; 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());
SpreadsheetApp.flush();
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(false);
form.setConfirmationMessage("Before your vote, the veto count was at 0. The system is processing your vote. Only your most recent vote from your uniqueID will be counted.");
//form.setCollectEmail(true);
var formID = form.getId();
var items = form.getItems();
var formResponse = form.createResponse();
// Prefill SessionId
var formItem = items[1].asTextItem();
/*
for(var q = 0; q < 2; q++){
var response = formItem.createResponse(uniqueIDList[q]);
formResponse.withItemResponse(response);
var url = formResponse.toPrefilledUrl();
var emailTo = "@tmomail.net";
var subject = "TRADE PROPOSED";
var body = theTrade + "\n \n Your secret login code is:" + uniqueIDList[q] + "\n Click this link to vote:" + url;
MailApp.sendEmail(emailTo, subject, body, {noReply: true});
}
*/
var submitFormTrigger = ScriptApp.newTrigger('onFormSubmit')
.forForm(form)
.onFormSubmit()
.create();
var timeFormTrigger = ScriptApp.newTrigger('timesUp')
.timeBased()
//.after(86400000)
//set to 5 minutes
.after(300000)
.create();
setupTriggerArguments(submitFormTrigger, [formID, theTrade], true);
setupTriggerArguments(timeFormTrigger, [formID, theTrade], false);
}
function timesUp(e) {
var functionArguments = handleTriggered(e.triggerUid);
var form = FormApp.openById(functionArguments[0]);
var theTrade = functionArguments[1];
form.setAcceptingResponses(false);
form.setAllowResponseEdits(false);
form.removeDestination();
form.setCustomClosedFormMessage("Sorry, the veto window on this trade is now closed because 24 hours has passed.")
var emailTo = "@tmomail.net";
var subject = "TRADE PASSED";
var body = "The following trade was PASSED because 24 hours has passed without 6 veto votes (ignore the intro text):" + theTrade;
MailApp.sendEmail(emailTo, subject, body, {noReply: true});
}
function onFormSubmit(e) {
Logger.log("%s", JSON.stringify(e));
var functionArguments = handleTriggered(e.triggerUid);
var form = FormApp.openById(functionArguments[0]);
var theTrade = functionArguments[1];
//var newResponse = {names: e.namedValues['Unique user ID (DO NOT CHANGE):'][0], vote: e.namedValues['How do you vote?'][0]};
//var masterSpreadsheet = SpreadsheetApp.openById('1XMWcwZEyPI47QcS9MblZS6p2w4NTKESvtORJTP-a_oU');
//form.setDestination(FormApp.DestinationType.SPREADSHEET,masterSpreadsheet.getId());
//SpreadsheetApp.flush()
var ssID = form.getDestinationId();
Logger.log(ssID);
var ss = SpreadsheetApp.getActiveSheet();
var lastRow = ss.getLastRow();
Logger.log(lastRow);
var data = ss.getRange('C2:C'+lastRow.toString()).getValues();
var lastRow = data.length - 1;
var names = data[lastRow];
lastRow = lastRow - 1;
for (var i = lastRow; i > -1; i--){
var duplicate = false
var newName = data[i];
var nameLength = names.length;
Logger.log(names);
for (var j = 0; j < nameLength; j++){
if (names[j].toString() == newName.toString()){
Logger.log(j);
duplicate = true;
ss.deleteRow(i+2);
break;
}
}
if (duplicate == false){
names.push(newName);
}
}
var lastRow = ss.getLastRow();
Logger.log(lastRow);
var vetoColumn = ss.getRange('B2:B'+lastRow.toString()).getValues();
var vetoCount = 0;
for (var m = 0; m < vetoColumn.length; m++){
if (vetoColumn[m].toString() == "Veto"){
vetoCount = vetoCount + 1;
}
}
form.setConfirmationMessage("Before your vote, the veto count was at "+ vetoCount + ". The system is processing your vote. Only your most recent vote from your uniqueID will be counted.");
// This is where the Veto Threshold is Set!!
if (vetoCount > 1){
form.setAcceptingResponses(false);
form.setAllowResponseEdits(false);
form.removeDestination();
form.setCustomClosedFormMessage("The veto window on this trade is now closed because the trade was vetoed.");
var emailTo = "@tmomail.net";
var subject = "TRADE VETOED";
var body = "Yo, this trade just got vetoed (ignore the intro text):" + theTrade;
MailApp.sendEmail(emailTo, subject, body, {noReply: true});
}
}