我创建了一个脚本,允许我使用电子表格中的数据在Google表单中创建动态下拉列表。电子表格中的数据是一个日期列表,但当它被添加到Google表格的下拉列表中时,它就变成了一个字符串。当有人提交回复时,数据会转到电子表格,而这个下拉值会被视为字符串而不是日期,例如'5/5/2018 而不是 5/5/2018 。
var fromRange = 'RequestDates'; //namedRange from Spreadsheet for 'From date' dropdown
var toRange = 'RequestToDates'; //namedRange from Spreadsheet for 'To date' dropdown
function updateDates(e){
// a way to get the items from the form
var form = FormApp.openById("10uyPYIpVtKLg5eaxIrc8di93v7NW1ZSL6gjbtQs4NvE");
var FromList = form.getItemById(1.403587334E9).asListItem();
var ToList = form.getItemById(4.09925665E8).asListItem();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var FormsTest = ss.getSheetByName("AppScript");
// get the values in the first column accept header row 1
var FromValues = FormsTest.getRange('RequestDates').getDisplayValues();
var ToValues = FormsTest.getRange('RequestToDates').getDisplayValues();
var FromNames = [];
for(var i = 0; i < FromValues.length; i++) {
if(FromValues[i][0] != "") {
FromNames[i] = FromValues[i][0];
}
}
var ToNames = [];
for(var y = 0; y < ToValues.length; y++) {
if(ToValues[y][0] != "") {
ToNames[y] = ToValues[y][0];
}
}
// populate the list
FromList.setChoiceValues(FromNames);
ToList.setChoiceValues(ToNames);
}
电子表格中的日期列表每天自动更新,仅允许当天后的12天到当天后的第180天,这意味着动态下拉也会每天自动更新。我对javascript很新,所以任何建议都会非常感激。
答案 0 :(得分:1)
试试这个:
function dateformat(e){ //I presume your in a spreadsheet getting an onFormatSubmit trigger
var sh=e.range.getSheet();
var rg = sh.getRange(2,3,sh.getLastRow(),1);
var data=rg.getValues(); //the plan would be to iterate over all of the rows I guess.
for(var i=0;i<data.length;i++){
var datstr=String(data[i][0]).slice(1);
data[i][0]=new Date(datstr);
}
rg.setValues(data);//This will replace the strings with the date values and you can set the format for that range anyway you wish.
}