我正在基于之前列中单元格中的值在列中的每个单元格中设置验证。如果我的脚本按每个单元一个一个地运行(无循环),则按我的意愿工作。通过脚本在单元格中设置验证并查看效果需要一秒钟左右的时间(我的意思是明显的缓慢)。当我将代码放入for
循环中时,它没有在某些单元格上完成任务,我认为由于完成在单元格上应用验证设置所需的时间。仅当验证设置在当前单元格中生效后,我如何才能控制for
循环才能再次循环?
我正在尝试在每个单元格中添加从属下拉列表。
中的代码//google scripts for google spreadsheet
function addDropDown(){
var tabLists = "Lists";
var tabValidation = "DATA";
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var datass = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(tabLists);
var datass0 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(tabValidation);
function flatten(arrayOfArrays){
return [].concat.apply([], arrayOfArrays);
}
var dists_in_data = datass0.getRange(5, 2,datass0.getLastRow()).getValues(); //Admin1 in data
var dists_in_list = datass.getRange(2, 9,datass.getLastRow()).getValues(); //Admin1 in Lists
dists_in_list = flatten(dists_in_list);
dists_in_data = flatten(dists_in_data);
for (var i in dists_in_data){
//i = 0
var search_key = dists_in_data[i];
var distStart = dists_in_list.indexOf(search_key) + 1;
dists_in_list.reverse();
var len = dists_in_list.length;
var distEnd = len - (dists_in_list.indexOf(search_key) + distStart - 1);
dists_in_list.reverse();
//preparing validation list for admin2
if(distStart != 0){
var validationRange = datass.getRange(distStart + 1, 10, distEnd );
var validationRule = SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).build();
datass0.getRange(i + 5, 3).setDataValidation(validationRule);
}
}
}
我希望所有单元格都应设置新的验证规则。