我有一个Google工作表脚本,用于为提交的googleforms生成网址。脚本运行良好,但现在提交的数量已大大增加,我一直收到“超过最大执行时间”的消息。
如何使我的脚本从1000行开始循环?还是N行?
或
如何使循环从上次更新的响应开始?
function onOpen(){
var menu = SpreadsheetApp.getUi();
menu.createMenu('Get URLs')
.addItem('Iniciar ', 'GetURL')
.addToUi();
}
function GerarURL() {
var formId = Browser.inputBox('https://docs.google.com/forms/d/xxxxxxxx/edit#responses', Browser.Buttons.OK)
if (formId == ''){
var formId = Browser.inputBox('URL not informed', Browser.Buttons.OK_CANCEL);
} else {
formId.toString();
var formulario = FormApp.openByUrl(formId)
var planilha = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var dados = planilha.getDataRange().getValues();
var respostas = formulario.getResponses();
var coluna = planilha.getRange(Browser.inputBox('GG:GG',
Browser.Buttons.OK_CANCEL)).getColumn();
var hora = [];
var urls = [];
var set = [];
for(var i=0; i<respostas.length; i++){
hora.push(respostas[i].getTimestamp().setSeconds(0));
urls.push(respostas[i].getEditResponseUrl());
}
for (var x = 1; x< dados.length; x++){
if(dados[x][0]){
set.push([urls[hora.indexOf(dados[x][0].setSeconds(0))]]);
} else {
[''];
};
planilha.getRange(2, coluna, set.length).setValues(set);
}
}
}
答案 0 :(得分:2)
首先,应通过向函数传递参数Form.getResponses(date)
来按日期限制响应。(仅在该日期之后才能获得响应。)
其次,将上次处理的日期存储在PropertiesService
或工作表中。我希望使用script property。
// provide a default value for when property "last" is null
var last = PropertiesService.getScriptProperties().getProperty("last") || "2019-08-01";
var respostas = formulario.getResponses(new Date(last));
// when finished
var dateString = Utilities.formatDate(new Date(), "America/New_York", "YYYY-MM-dd");
PropertiesService.getScriptProperties().setProperty("last", dateString);
答案 1 :(得分:0)
function onOpen(){
var menu = SpreadsheetApp.getUi();
menu.createMenu('Get URLs')
.addItem('Iniciar ', 'GetURL')
.addToUi();
}
function GerarURL() {
var formId = Browser.inputBox('https://docs.google.com/forms/d/xxxxxxxx/edit#responses', Browser.Buttons.OK)
if (formId == ''){
var formId = Browser.inputBox('URL not informed', Browser.Buttons.OK_CANCEL);
} else {
formId.toString();
var formulario = FormApp.openByUrl(formId)
var planilha = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var dados = planilha.getRange(1,1,planilha.getLastRow(),1).getValues();
var zeroSeconds=[]
for(var i=0; i<dados.length;i++)
{
zeroSeconds.push(dados[i][0].setSeconds(0))
}
var respostas = formulario.getResponses();
var coluna = planilha.getRange(Browser.inputBox('GG:GG', Browser.Buttons.OK_CANCEL)).getColumn();
var hora =respostas[respostas.length-1].getTimestamp().setSeconds(0);
var url = respostas[respostas.length-1].getEditResponseUrl();
if(zeroSeconds.indexOf(hora)!=(-1)){
planilha.getRange(zeroSeconds.indexOf(hora)+1, coluna).setValue(url);
}
}
}
原始代码更改为:
希望对您有帮助。