【发布时间】:2021-11-26 07:49:33
【问题描述】:
表“主要”包含所有生产数据,如生产日期、机器、产品、生产数量等。在另一张表“AT Guidecard Tracking”中,我正在输入生产日期和机器。我希望第三列以下拉列表的形式自动提供针对输入的机器和生产日期组合运行的产品。同一天同一台机器上可能有两个或多个产品。我在应用程序脚本中编写了以下代码:
// Get active sheet
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var listMain = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Main");
// Get active cell in active sheet
var activeCell = ss.getActiveCell()
if(activeCell.getColumn() == 2 && activeCell.getRow() > 1 && ss.getName() == "AT Guidecard Tracking"){
activeCell.offset(0,1).clearContent().clearDataValidations();
if(activeCell.isBlank() == false){
var machine = activeCell.getValue(); // Col 2
var prodDate = activeCell.offset(0,-1).getValue(); // Col 1
//Logger.log(prodDate);
// Filter main proudction data based on machine and date
var MainData = listMain.getRange(2,1,listMain.getLastRow()-1,13).getValues();
//Logger.log(MainData);
// compute prod type using FILTER
var prodTypeList = MainData.filter(function(item){
return item[0] == prodDate && item[1] === machine;
})
var distinct = (value,index,self) => {
return self.indexOf(value) === index;
}
var prodTypeValidationList = prodTypeList.map(x => x[3]).filter(distinct).sort();
var prodTypeValidationRule = SpreadsheetApp.newDataValidation().requireValueInList(prodTypeValidationList).setAllowInvalid(false).build();
// Apply validation rule to adjacent cell using offset
activeCell.offset(0,1).setDataValidation(prodTypeValidationRule);
}
}
但是,当我运行它时,我在 Col 3(数据验证)中得到一个空列表。工作表“Main”和“AT Guidecard Tracking”中的日期列都被格式化为日期。
例如,我在工作表“AT Guidecard Tracking”和机器 MS-02 中输入日期 19/07/2021(存储在变量 prodDate 中)。此组合的“主要”工作表中有相应的条目。当我调试并查看存储在变量MainData 上的数据时,日期完全匹配! (见附图)。
为什么过滤器返回一个空数组?
【问题讨论】:
-
尝试删除其中一项,看看数据是否返回
item[0] == prodDate && item[1] === machine;
标签: javascript google-apps-script google-sheets