作業表“Main”包含所有生產資料,如生產日期、機器、產品、生產數量等。在另一個作業表“AT Guidecard Tracking”中,我正在輸入生產日期和機器。我希望第 3 列以下拉串列的形式自動提供為輸入的機器和生產日期組合運行的產品。同一日期同一臺機器上可能有兩個或多個產品。我在應用程式腳本中撰寫了以下代碼:
// 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);
}
}
但是,當我運行它時,我在第 3 列(資料驗證)中得到一個空串列。作業表“Main”和“AT Guidecard Tracking”中的日期列都被格式化為日期。
例如,我19/07/2021在作業表“AT Guidecard Tracking”和 Machine 中輸入日期(存盤在變數 prodDate 中)MS-02。在“Main”表中有一個與此組合對應的條目。當我除錯并查看存盤在變數上的資料時MainData,日期完全匹配!(見附圖)。

為什么過濾器回傳一個空陣列?
uj5u.com熱心網友回復:
我猜這永遠不會是真的,item[0] == prodDate因為一個 Date() 物件永遠不會等于另一個 Date() 物件,但是如果你比較 valueOf() 或 getTime() 那么你可以進行那種比較。
嘗試這個:
var prodDate = new Date(activeCell.offset(0,-1).getValue()).valueOf(); // 第 1 列
然后嘗試 new Date(item[0]).valueOf() == prodDate
請注意兩個日期的日期時間完全相同
回答你的問題:
let dt = new Date();
let today = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate()).valueOf();
let yesterday = new Date(dt.getFullYear(),dt.getMonth(),dt.getDate() - 1).valueOf();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/366116.html
上一篇:如何為子反應組件設定css樣式?
