我正在努力弄清楚為什么我在這里使用的 Javascript includes() 函數(shù)不匹配并返回true. 該代碼在 Google 表格上的 Google 腳本中用于填充一些單元格值。這是完整腳本中的相關(guān)代碼:const currentYear = getCurrentYear();//Get current year from active sheet namefunction getCurrentYear(){ year = sheetName.slice(-7, -2); return year; //returns "2020"}//Return Current Control File IDfunction getCurrentControlFileId(){ const controlFolder = DriveApp.getFolderById(controlFolderId); const controlFiles = controlFolder.getFiles(); while (controlFiles.hasNext()){ let controlFile = controlFiles.next(); let currentControlFileName = controlFile.getName(); // Evaluates to "CONTROL 2020" let searchString = `${currentYear}`; // Evaluates to "2020" //Have also tried: //let searchString = currentYear; if (currentControlFileName.includes(searchString)){ let controlFileId = controlFile.getId(); return controlFileId; } } }controlFile.getName().includes(searchString)false即使controlFile.getName()值為“CONTROL 2020”且searchString值為“2020”,也會返回。兩者都是字符串。if如果我像這樣在語句條件中手動輸入“2020” :controlFile.getName().includes("2020")它返回 true 并按預(yù)期工作,返回 ID 字符串。我也嘗試過 wrapping currentYearand controlFile.getName()inString()但它仍然返回 false。我真的看不出這里有什么問題,希望得到一些幫助。
Google Script / Javascript includes()函數(shù)在預(yù)期有效輸入時不匹配
嗶嗶one
2023-03-24 14:01:41