
[英]Match Entire Cell for Multiple Find and Replace in Google Sheets via Google Apps Script
[英]How to change multiple cell values in Google Sheets via Apps Script?
我想編寫一個腳本來格式化打印的生產計划。 我想輸入單個字母,然后運行腳本將這些單個字母更改為單詞並使用邊框等格式化單元格。
以下 if/else if/else 操作對單個單元格執行我想要的操作:
var TESTalpha = printCHSheet.getRange(4, 5, 1, 1);
var TESTalphaData = TESTalpha.getValue();
if (TESTalphaData == 'f') {
TESTalpha.clear();
TESTalpha.setValue('FAB').setBorder(true, true, true, true, true, true);
} else if (TESTalphaData == 'p') {
TESTalpha.clear();
TESTalpha.setValue('PAINT').setBorder(true, true, true, true, true, true);
} else if (TESTalphaData == 'c') {
TESTalpha.clear();
TESTalpha.setValue('FINISH').setBorder(true, true, true, true, true, true);
} else {
TESTalpha.setValue(null);
}
最終,我想將此應用於行和列中的一系列單元格,但首先我嘗試評估一行中的五個單元格。
下面的循環運行,但用它找到的最后一個值替換每個單元格(僅供參考……五個單元格 E4:I4 填充了 f/ f/ p/ c/ c):
var TESTalpha = printCHSheet.getRange(4, 5, 1, 5);
var TESTlength = TESTalpha.getLastColumn();
var TESTalphaData = TESTalpha.getValues();
for (var i=0; i < TESTlength+1; i++) {
if (TESTalphaData[0][i] == "f") {
TESTalpha.clear();
TESTalpha.setValue("FAB");
} else if (TESTalphaData[0][i] == "p") {
TESTalpha.clear();
TESTalpha.setValue("PAINT");
} else if (TESTalphaData[0][i] == "c") {
TESTalpha.clear();
TESTalpha.setValue("FINISH");
}
}
我在這里得到的結果是 E4:I4 范圍內的所有單元格都設置為 FAB; 然后全部設置為PAINT; 然后全部設置為FINISH。 相反,我想要的是 FAB/FAB/PAINT/FINISH/FINISH。 我知道上面的腳本正在替換所有單元格,因為我正在對其進行編程,但是我的各種修改都失敗了,所以我將它發布在這里,因為它可能朝着正確的方向發展。
我的另一個想法是運行一個循環來拼接數組,然后清除范圍並設置(或推送)循環修改的新數組:
var TESTalpha = spreadsheet.getRange("E4:I4");
var TESTlength = TESTalpha.getLastColumn();
for (var i=0; i < TESTlength+1; i++) {
var TESTalphaData = TESTalpha.getValues();
if (TESTalphaData[0][i] == "f") {
TESTalphaData.splice([i], 1, "FAB");
} else if (TESTalphaData[0][i] == "p") {
TESTalphaData.splice([i], 1, "PAINT");
} else if (TESTalphaData[0][i] == "c") {
TESTalphaData.splice([i], 1, "FINISH");
}
spreadsheet.getRange("E4:I4").clear();
spreadsheet.getRange("E4:I4").setValues(TESTalphaData);
}
上面的問題是(至少)它是一個二維數組,但我試圖像一維字符串數組一樣拼接。
我也試過一個開關,但沒有成功。
在此先感謝您的幫助!
不要只是復制和使用它,它有注釋,所以你知道下次該怎么做:
var TESTalpha = spreadsheet.getRange("E4:I4"),
TESTalphaData = TESTalpha.getValues();
var col = TESTalphaData[0].length; // use a meaningful name for a variable
while( --col >= 0 ) {
// Make the tests and assign the variable the array you pulled from the sheet with getValues(), it's exactly the same you need to input with setValues()
if (TESTalphaData[0][col ] == "f") {
TESTalphaData[0][col] = 'FAB';
} else if (TESTalphaData[0][col] == "p") {
TESTalphaData[0][col] = "PAINT";
} else if (TESTalphaData[0][col] == "c") {
TESTalphaData[0][col] = "FINISH";
}
// Setting the values should be after the loop is complete, so I removed it from inside the for (now a while), to the end of the code
}
// No need to get the Ranges again, you already save it up there
// No need to clear the range, it will all be re-written again
TESTalpha.setValues(TESTalphaData);
部分答案(仍然需要幫助僅在包含值的單元格周圍放置邊框)。 這個while
循環在另一個while
內循環更改由行和列組成的多維數組的值。 非常感謝@Kriggs。
var TESTalpha = printCHSheet.getRange(4, 5, 7, 7),
TESTalphaData = TESTalpha.getValues(),
rowOp = TESTalpha.getNumRows();
while ( --rowOp >= 0 ) {
var col = TESTalphaData[rowOp].length;
while( --col >= 0 ) {
if (TESTalphaData[rowOp][col] == "f") {
TESTalphaData[rowOp][col] = "FAB";
} else if (TESTalphaData[rowOp][col] == "p") {
TESTalphaData[rowOp][col] = "PAINT";
} else if (TESTalphaData[rowOp][col] == "c") {
TESTalphaData[rowOp][col] = "FINISH";
}
}
}
TESTalpha.setValues(TESTalphaData);
問題是, TESTalphaData 是一個數組,所以我不能用它設置邊框。 我可以使用 TESTalpha 設置邊框(b/c 它是一個范圍),但隨后我得到的邊框恰好是每行 7 個單元格(因為我定義了范圍)。
我怎樣才能運行另一個循環來做這樣的事情(這不起作用)? if (rangeX != null) {rangeX.setBorder(true, true, true, true, true, true);
同樣,范圍不會檢查值,但我無法為值設置邊框,因為它們存在於除單元格之外的數組中。
真的很感激任何幫助!
這樣做! 如果單元格的空白是假的,則for
循環內的for
循環設置邊框 -換句話說,如果它已填充。
for (var r = 4; r < 50; r++) {
for (var c = 5; c < 21; c++) {
var singleCell = printCHSheet.getRange(r, c, 1, 1);
var singleCellBlank = singleCell.isBlank();
if ( !singleCellBlank ) {
singleCell.setBorder(true, true, true, true, true, true)
}
}
}
我很高興。 現在我要做的就是格式化一些字體,將其他字母添加到else if
評估等,我就完成了!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.