【发布时间】:2019-12-29 16:48:18
【问题描述】:
这个脚本似乎运行得很好。这个问题更针对它是如何编写的(由初学者)。
我一直在编写一个脚本,在将它分解为多个函数后,我很难将变量从第一个函数传递到下一个函数。阅读后,我发现我不一定需要包含“var =”,尽管我不能 100% 确定其中的区别。我设法让“变量”(它们仍然被视为变量吗?)传递给以下函数,但我只是想确保我所做的事情是有效/可接受的。
function onEdit(e){
/* I switched these from "var = " because they weren't passing
down to the following functions.
*/
activess = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
activeCell = activess.getActiveCell();
valueToFind = activeCell.getValue();
// Column Values
foundItemValues = [];
foundSubItemValues = [];
foundCatValues = [];
foundSubCatValues = [];
// These never change regardless of active sheet
catss = SpreadsheetApp.getActive().getSheetByName('Categories-Concat');
catData = catss.getRange(1,2,catss.getLastRow(),catss.getLastColumn()).getValues();
catIndex = catData[0].indexOf(activeCell.getValue()) + 2;
subCatIndex = catData[0].indexOf(activeCell.getValue()) + 2;
itemss = SpreadsheetApp.getActive().getSheetByName('Items');
itemdata = itemss.getRange(2,1,itemss.getLastRow(),4).getValues();
if(e.range.getSheet().getName() == 'projectSelections'){
activess = e.range.getSheet().getName();
colCss = SpreadsheetApp.getActive().getSheetByName('Categories-Concat');
colCdata = colCss.getRange(1,2,1,colCss.getLastColumn()).getValues();
colCIndex = colCdata[0].indexOf(activeCell.getValue()) + 2;
if(activeCell.getColumn() == 2 && activeCell.getRow() > 1){
this.subCategoryDV(e);
}
}
}
function subCategoryDV(e){
// Populate SUB-CATEGORY data validations
activeCell.offset(0, 1).clearDataValidations();
for (var q = 1; q < catData.length; q++){
for(var i=0;i<catData.length;i++){
if(valueToFind==catData[0][i]){
foundSubCatValues.push(catData[q][i]);
}
}
}
var subCatValidationRange = foundSubCatValues;
var subCatValidationRule = SpreadsheetApp.newDataValidation().requireValueInList(subCatValidationRange).build();
if(activeCell.getValue() != ""){
activeCell.offset(0, 1).setDataValidation(subCatValidationRule);
}
}
【问题讨论】:
-
我想这确实回答了我的整体问题,但我也希望对我所做工作的整体结构有所了解。但我会接受“这解决了我的问题”。谢谢!
-
对于“整体结构的洞察”,请尝试Code Review。
标签: javascript google-apps-script google-sheets global-variables var