我正在研究一个基于不同条件(依赖数据验证)进行数据验证的电子表格。我们刚刚创建并且脚本已经更新了。但是,我无法使其在不同的选项卡上工作(脚本适用于每个选项卡)。现在我已经更新了三个不同选项卡的脚本,但是,我的脚本仅适用于我在“Arvind”名称中创建的最后一个。你可以帮助我使用相同的脚本在应用程序中使用不同的名称使不同的选项卡工作吗?复制(授权)脚本如下:
function onEdit(e) {
var sh = e.source.getActiveSheet(),
allValues, list,
/*'easy to change' variables*/
sheet = 'James Main Sheet',
sheetWithLists = 'Type of Request',
rangeWithLists = 'B1:H', //assuming the first row to be the header row
colValidation = 2, //the column with the 'first' validation
secondValidationOffset = 1; //the offset from the above column / the column with the 'second' validation
/*check conditions*/
if (sh.getName() !== sheet || e.range.columnStart !== colValidation || e.range.rowStart < 2 || typeof e.value == 'object') return;
/*get all values from the sheet with the lists (cached after the first run)*/
allValues = getFromCache_(sheetWithLists, rangeWithLists)
/*get the correct list(column) and remove the header*/
list = allValues.map(function (v, i) {
return v[allValues[0].indexOf(e.value)]
}).splice(1);
/*set the validation in offset column*/
e.range.offset(0, secondValidationOffset)
.setDataValidation(SpreadsheetApp.newDataValidation()
.requireValueInList(list)
.build());
}
/*Using caching mechanism to limit the calls to the sheet with the lists --> faster execution*/
function getFromCache_(sheetName, range) {
var key = 'DD_' + sheetName,
c = CacheService.getPublicCache(),
d,
t = c.get(key);
if (t) {
d = JSON.parse(t);
} else {
d = SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName(sheetName)
.getRange(range)
.getValues();
c.put(key, JSON.stringify(d));
}
return d;
}
//Script courtesy of Top Contributor Jean-Pierre Verhulst
答案 0 :(得分:0)
您的代码仅在最后一个工作表中工作的原因是由于以下两行代码:
sheet = 'James Main Sheet'
...
if (sh.getName() !== sheet || e.range.columnStart !== colValidation || e.range.rowStart < 2 || typeof e.value == 'object') return;
工作表变量设置为James Main sheet
因此仅适用于该工作表。 if函数查看您是否正在编辑的工作表是通过此sh.getName() !== sheet
的工作表,如果名称不匹配,则使用return
终止该函数。
假设您在除Type of request
表之外的所有工作表上使用此代码。您将代码修改为此
sheet = 'Type of request'
...
if (sh.getName() == sheet || e.range.columnStart !== colValidation || e.range.rowStart < 2 || typeof e.value == 'object') return;
最终代码如下所示:
function onEdit(e) { var sh = e.source.getActiveSheet(), allValues, list,
/*'easy to change' variables*/
sheet = 'Type of request',
sheetWithLists = 'Type of Request',
rangeWithLists = 'B1:H', //assuming the first row to be the header row
colValidation = 2, //the column with the 'first' validation
secondValidationOffset = 1; //the offset from the above column / the column with the 'second' validation
/*check conditions, exit function if the sheet name is 'type of request'*/
if (sh.getName() == sheet || e.range.columnStart !== colValidation || e.range.rowStart < 2 || typeof e.value == 'object') return;
/*get all values from the sheet with the lists (cached after the first run)*/
allValues = getFromCache_(sheetWithLists, rangeWithLists)
/*get the correct list(column) and remove the header*/
list = allValues.map(function (v, i) {
return v[allValues[0].indexOf(e.value)]
}).splice(1);
/*set the validation in offset column*/
e.range.offset(0, secondValidationOffset)
.setDataValidation(SpreadsheetApp.newDataValidation()
.requireValueInList(list)
.build());
}
/Using caching mechanism to limit the calls to the sheet with the lists --> faster execution/ function getFromCache_(sheetName, range) {
var key = 'DD_' + sheetName,
c = CacheService.getPublicCache(),
d,
t = c.get(key);
if (t) {
d = JSON.parse(t);
} else {
d = SpreadsheetApp.getActiveSpreadsheet()
.getSheetByName(sheetName)
.getRange(range)
.getValues();
c.put(key, JSON.stringify(d));
}
return d;
}