[英]Google Apps Script or JavaScript - Find & Replace result is one row higher than required - How do I move it one row up to replace?
項目背景
努力為我的作品的庫存清單創建一個動態的 W/B。
有多個帶有數據的 W/S 和兩個用戶表單來搜索、編輯和插入新數據到相關位置。
上面顯示了 Warehouse Form 以及此任務所需的 Sales WS。 我已成功調用了從 Sales WS 到 Userform 的字段“Cuts”標記為 False 的所有記錄。
目標單擊按鈕(分配給用戶窗體上顯示的“全部剪切”按鈕),目標是:
a) 在 Sales WS 中找到與 Warehouse Userform 上給定行匹配的相應行。 (代碼使用兩個循環成功地做到了這一點。)
b) 在 Sales WS 上找到的行中,將“Cuts”列從 false 更改為 true。
當前代碼
//Update to "All Cut" information on the Sales Form
function updateCuts() {
let ss = SpreadsheetApp.getActiveSpreadsheet()
let shUserForm = ss.getSheetByName("Warehouse Form")
let shSalesSheet = ss.getSheetByName("Sales")
const sData = shSalesSheet.getDataRange().getValues();
const wData = shUserForm.getRange(9,3,shUserForm.getLastRow()+1,shUserForm.getLastColumn()+1).getValues();
for (var i=9; i<wData.length; i++) {
var wsRow = wData[i];
const sValue = shUserForm.getRange("C" + i).getValue();
const tValue = shUserForm.getRange("E" + i).getValue();
const wValue = shUserForm.getRange("G" + i).getValue();
if(shUserForm.getRange("C" + i).isBlank()==false){
for(var j=0; j<sData.length; j++) {
var srow = sData[j];
if (srow[0]===sValue && srow[1]===tValue && srow[2]===wValue) {
shSalesSheet.getRange("E" + j).setValue(true) // Change Cut Checkbox from False to True
}}
i += 2}}
}
當前結果/問題
顯示的代碼正在更改找到的記錄上方行中的“Cuts”列值,而不是所需的記錄(如圖 Sales WS #2 所示)
問題
如何更改代碼以解決此問題?
您需要在E(j+1)
setValue()
設置值(),因為j
是從 0 開始的索引,並且行是從 1 開始的。
function updateCuts() {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet()
const userForm = spreadsheet.getSheetByName("Warehouse Form")
const salesSheet = spreadsheet.getSheetByName("Sales")
const salesData = salesSheet.getDataRange().getValues()
userForm.getRange(9, 3, shUserForm.getLastRow()-8, 10)
.getValues()
.filter(row => row[0] !== ``)
.forEach(entry => {
const index = salesData.findIndex(row => row[0] === entry[0] && row[1] === entry[2] && row[2] === entry[4])
if (index !== -1) salesSheet.getRange(`E${index+1}`).setValue(true)
})
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.