javascript 预填充URL 2.0.2备份
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 预填充URL 2.0.2备份相关的知识,希望对你有一定的参考价值。
// Gets range, get values, get row, pass them to prefillUrls function
function ZZrunPrefillUrls0(){
var valuesArray = ZZdefineValues();
var rowsArray = ZZdefineRows();
ZZprefillUrls(valuesArray[0],rowsArray[0]);
}
function ZZrunPrefillUrls1(){
var valuesArray = ZZdefineValues();
var rowsArray = ZZdefineRows();
ZZprefillUrls(valuesArray[1],rowsArray[1]);
}
function ZZrunPrefillUrls2(){
var valuesArray = ZZdefineValues();
var rowsArray = ZZdefineRows();
ZZprefillUrls(valuesArray[2],rowsArray[2]);
}
function ZZrunPrefillUrls3(){
var valuesArray = ZZdefineValues();
var rowsArray = ZZdefineRows();
ZZprefillUrls(valuesArray[3],rowsArray[3]);
}
function ZZrunPrefillUrls4(){
var valuesArray = ZZdefineValues();
var rowsArray = ZZdefineRows();
ZZprefillUrls(valuesArray[4],rowsArray[4]);
}
function ZZrunPrefillUrls5(){
var valuesArray = ZZdefineValues();
var rowsArray = ZZdefineRows();
ZZprefillUrls(valuesArray[5],rowsArray[5]);
}
function ZZrunPrefillUrls6(){
var valuesArray = ZZdefineValues();
var rowsArray = ZZdefineRows();
ZZprefillUrls(valuesArray[6],rowsArray[6]);
}
function ZZrunPrefillUrls7(){
var valuesArray = ZZdefineValues();
var rowsArray = ZZdefineRows();
ZZprefillUrls(valuesArray[7],rowsArray[7]);
}
function ZZdefineRanges() {
// var ss = SpreadsheetApp.openById("1rOFglBpMlynWo_00mZXtMR3AnGX29XILHl0gUgA416I"); // for development and debugging
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("requiredSectionSheet");
var totalRange = sheet.getDataRange();
var totalRows = totalRange.getNumRows();
var totalColumns = totalRange.getNumColumns();
// Subdivides the total sheet into 8 sections (times out otherwise)
var eighthRows = Math.floor(totalRows * 0.125);
var quarterRows = Math.floor(totalRows * 0.25);
var threeEighthsRows = Math.floor(totalRows * 0.375);
var halfRows = Math.floor(totalRows * 0.5);
var fiveEighthsRows = Math.floor(totalRows * 0.625);
var threeQuarterRows = Math.floor(totalRows * 0.75);
var sevenEighthsRows = Math.floor(totalRows * 0.875);
// Defines each quarter range and gets the values
var range1Rows = sheet.getRange(1,1,eighthRows,totalColumns);
var range2Rows = sheet.getRange(eighthRows,1, quarterRows - eighthRows + 1, totalColumns);
var range3Rows = sheet.getRange(quarterRows,1, threeEighthsRows - quarterRows + 1, totalColumns);
var range4Rows = sheet.getRange(threeEighthsRows,1, halfRows - threeEighthsRows + 1, totalColumns);
var range5Rows = sheet.getRange(halfRows,1, fiveEighthsRows - halfRows + 1, totalColumns);
var range6Rows = sheet.getRange(fiveEighthsRows,1, threeQuarterRows - fiveEighthsRows + 1, totalColumns);
var range7Rows = sheet.getRange(threeQuarterRows,1, sevenEighthsRows - threeQuarterRows + 1, totalColumns);
var range8Rows = sheet.getRange(sevenEighthsRows,1, totalRows - sevenEighthsRows + 1, totalColumns);
var rows = [range1Rows,range2Rows,range3Rows,range4Rows,range5Rows,range6Rows,range7Rows,range8Rows];
return rows;
}
function ZZdefineValues(){
var v = ZZdefineRanges();
var vArray = [];
for(var x = 0; x < v.length;x++){
vArray.push(v[x].getValues());
}
return vArray;
}
function ZZdefineRows(){
var r = ZZdefineRanges();
var rArray = [];
for(var x = 0; x < r.length;x++){
rArray.push(r[x].getRow());
}
return rArray;
}
// Defines a consistent headerRange (since the ranges 2-n don't include the header)
function ZZdefineHeaderRange(){
// var ss = SpreadsheetApp.openById("1rOFglBpMlynWo_00mZXtMR3AnGX29XILHl0gUgA416I"); // for development and debugging
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("requiredSectionSheet");
var headerRange = sheet.getDataRange().getValues()[0];
return headerRange;
}
function ZZprefillUrls(v,r){
// Sheet /////////////////////////////////////////////////////////////////////////////////
var ss = SpreadsheetApp.getActiveSpreadsheet();
var responseSheet = ss.getSheetByName("Form Responses 1");
var requiredSectionSheet = ss.getSheetByName("requiredSectionSheet");
var testSheet = ss.getSheetByName("testSheet");
var testSheetHeaders = testSheet.getDataRange().getValues()[0];
var requiredSectionSheetData = v; // gets all the values of the dataRange-- change this if the data is coming from a different sheet
var requiredSectionSheetHeaders = defineHeaderRange(); // gets the header row
var requiredSectionSheetHeadersLength = requiredSectionSheetHeaders.length;
///// Sheet Arrays /////
var prefilledResponseArray = []; // creates the array of responses to use as values for the prefill
var requiredSectionSheetLastRow = requiredSectionSheetData.length;
var prefilledUrlArray = []; // creates an array of the prefilled URLs to paste onto the spreadsheet
for (var d = 0; d < requiredSectionSheetLastRow; d++) { // change the source sheet here and data above
prefilledResponseArray.push(requiredSectionSheetData[d]);
} // for d
// Form /////////////////////////////////////////////////////////////////////////////////
var formUrl = SpreadsheetApp.getActiveSpreadsheet().getFormUrl(); // gets the form URL that is attached to the spreadsheet
var formId = FormApp.openByUrl(formUrl).getId(); // gets the id of the form
var form = FormApp.openById(formId); // opens the form based on its id
// Response /////////////////////////////////////////////////////////////////////////////////
///// Form Arrays /////
var titleArray = [];
var idArray = []; // creates an array of the ids for each item
var asTypeArray = []; // creates an array of the type of each item
var formLength = form.getItems().length;
for (var x = 0; x < formLength; x++) {
titleArray.push(form.getItems()[x].getTitle());
idArray.push(form.getItems()[x].getId());
asTypeArray.push(form.getItems()[x].getType());
}
var testSheetIndexOfPrefillUrls = testSheetHeaders.indexOf("Prefill URLs");
for (var s = 0; s < formLength; s++) { // removes page breaks and section headers--these don't need to be prefilled
if (asTypeArray[s] == 'PAGE_BREAK' || asTypeArray[s] == 'SECTION_HEADER') {
titleArray.splice(s,1);
idArray.splice(s, 1);
asTypeArray.splice(s, 1);
}
}
var formRequiredSectionIdArray = [];
var newAsTypeArray = [];
for (var x = 0; x < requiredSectionSheetHeadersLength; x++) {
formRequiredSectionIdArray.push(idArray[titleArray.indexOf(requiredSectionSheetHeaders[x])]); // needs to be an ID-- id index = index of header value in the title array
newAsTypeArray.push(asTypeArray[titleArray.indexOf(requiredSectionSheetHeaders[x])]); // needs to be a type-- type index = index of header value in the title array
}
var formRequiredSectionIdArrayLength = formRequiredSectionIdArray.length;
var formResponse = form.createResponse(); // Create a form response object--this is the bus
for (var x = 0; x < requiredSectionSheetLastRow - 1; x++) { // last row of where the array is-- minus 1 because of headers
for (var y = 0; y < formRequiredSectionIdArrayLength; y++) { // finds the index of -Prefill URLs- and iterates that number of times
// Creates a DYNAMIC itemType from the asTypeArray in the corresponding index
switch (newAsTypeArray[y]) { // needs a type
case FormApp.ItemType.CHECKBOX:
var formItem = form.getItemById(formRequiredSectionIdArray[y]).asCheckboxItem(); // these need an ID
break;
case FormApp.ItemType.MULTIPLE_CHOICE:
var formItem = form.getItemById(formRequiredSectionIdArray[y]).asMultipleChoiceItem();
break;
case FormApp.ItemType.LIST:
var formItem = form.getItemById(formRequiredSectionIdArray[y]).asListItem();
break;
case FormApp.ItemType.PARAGRAPH_TEXT:
var formItem = form.getItemById(formRequiredSectionIdArray[y]).asParagraphTextItem();
break;
case FormApp.ItemType.TEXT:
var formItem = form.getItemById(formRequiredSectionIdArray[y]).asTextItem();
break;
case FormApp.ItemType.DATETIME:
var formItem = form.getItemById(formRequiredSectionIdArray[y]).asDateTimeItem();
break;
case FormApp.ItemType.DATE:
var formItem = form.getItemById(formRequiredSectionIdArray[y]).asDateItem();
break;
case FormApp.ItemType.TIME:
var formItem = form.getItemById(formRequiredSectionIdArray[y]).asTimeItem();
break;
case FormApp.ItemType.DURATION:
var formItem = form.getItemById(formRequiredSectionIdArray[y]).asDurationItem();
break;
case FormApp.ItemType.GRID:
var formItem = form.getItemById(formRequiredSectionIdArray[y]).asGridItem();
break;
case FormApp.ItemType.SCALE:
var formItem = form.getItemById(formRequiredSectionIdArray[y]).asScaleItem();
break;
case FormApp.ItemType.CHECKBOX_GRID:
var formItem = form.getItemById(formRequiredSectionIdArray[y]).asCheckboxGridItem();
break;
case FormApp.ItemType.IMAGE:
var formItem = form.getItemById(formRequiredSectionIdArray[y]).asImageItem();
break;
default:
break;
} // switch
try { var response = formItem.createResponse([ prefilledResponseArray[x+1][y] ]); // creates a response for each row in given sheet--- make sure to wrap the createResponse in []
formResponse.withItemResponse(response); /* adds it to the form response (has all the passengers get on the bus)*/} catch(err){/*Logger.log("Error at Row: " + x + "Column: " + y)*/;continue;}
} // for y
prefilledUrlArray.push(formResponse.toPrefilledUrl()); // puts the response into a URL (parking spot)
} // for x
var prefilledUrlArrayLength = prefilledUrlArray.length;
// var prefillLastRow = ss.getRange("E1:E").getValues().filter(String).length;
for (var d = 0; d < prefilledUrlArrayLength; d++) {
var prefillLastRow = ss.getRange("E1:E").getValues().filter(String).length;
testSheet.getRange(1 + d + r, testSheetIndexOfPrefillUrls + 1).setValue(prefilledUrlArray[d]); // puts the array of urls into the given sheet (parking lot)
} // for d
} // function
以上是关于javascript 预填充URL 2.0.2备份的主要内容,如果未能解决你的问题,请参考以下文章