[英]How to change multiple cell values in Google Sheets via Apps Script?
I want to write a script to format a production schedule for printing.我想编写一个脚本来格式化打印的生产计划。 I want to enter single letters, then run the script to change those single letters into words and format the cells with borders, etc.我想输入单个字母,然后运行脚本将这些单个字母更改为单词并使用边框等格式化单元格。
The following if/ else if/ else operation does what I want for a single cell:以下 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);
}
Eventually I want to apply this to a range of cells in rows and columns, but first I am trying to evaluate five cells on a single row.最终,我想将此应用于行和列中的一系列单元格,但首先我尝试评估一行中的五个单元格。
The following loop runs, but replaces every cell with the last value it finds (FYI...the five cells E4:I4 are populated with f/ f/ p/ c/ c):下面的循环运行,但用它找到的最后一个值替换每个单元格(仅供参考……五个单元格 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");
}
}
The result I get here is that all cells in range E4:I4 are set to FAB;我在这里得到的结果是 E4:I4 范围内的所有单元格都设置为 FAB; then all set to PAINT;然后全部设置为PAINT; then all set to FINISH.然后全部设置为FINISH。 What I want to have, rather, is FAB/ FAB/ PAINT/ FINISH/ FINISH.相反,我想要的是 FAB/FAB/PAINT/FINISH/FINISH。 I know the above script is replacing all the cells because I'm programming it to, but my various modifications have failed, so I post it here as possibly heading in the right direction.我知道上面的脚本正在替换所有单元格,因为我正在对其进行编程,但是我的各种修改都失败了,所以我将它发布在这里,因为它可能朝着正确的方向发展。
Another idea I had is to run a loop to splice the array, then clear the range and set (or push) the new array modified by the loop:我的另一个想法是运行一个循环来拼接数组,然后清除范围并设置(或推送)循环修改的新数组:
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);
}
The problem with the above is (at least) that it's a 2D array, yet I'm trying to splice like a 1D array of strings.上面的问题是(至少)它是一个二维数组,但我试图像一维字符串数组一样拼接。
I've tried a switch, too, without success.我也试过一个开关,但没有成功。
Thanks in advance for any assistance!在此先感谢您的帮助!
Don't just copy and use it, it's commented so you know what to do next time:不要只是复制和使用它,它有注释,所以你知道下次该怎么做:
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);
Partial answer (still need help on putting borders around only the cells that contain a value).部分答案(仍然需要帮助仅在包含值的单元格周围放置边框)。 This while
loop inside of another while
changes values of the multiple dimensional array consisting of rows and columns.这个while
循环在另一个while
内循环更改由行和列组成的多维数组的值。 Many thanks to @Kriggs.非常感谢@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);
The thing is, TESTalphaData is an array, so I can't set borders with it.问题是, TESTalphaData 是一个数组,所以我不能用它设置边框。 I can set borders with TESTalpha (b/c it's a range), but then I get borders around exactly 7 cells per row (as I defined the range).我可以使用 TESTalpha 设置边框(b/c 它是一个范围),但随后我得到的边框恰好是每行 7 个单元格(因为我定义了范围)。
How can I run another loop to do something like this (which doesn't work)?我怎样才能运行另一个循环来做这样的事情(这不起作用)? if (rangeX != null) {rangeX.setBorder(true, true, true, true, true, true);
Again, a range won't check values, but I can't set borders for values, since they exist in arrays apart from cells.同样,范围不会检查值,但我无法为值设置边框,因为它们存在于除单元格之外的数组中。
Really appreciate any help!真的很感激任何帮助!
This does it!这样做! A for
loop inside a for
loop to set borders if it's false that the cell's blank-- In other words , if it's populated.如果单元格的空白是假的,则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)
}
}
}
I'm elated.我很高兴。 Now all I've got to do is format some fonts, add other letters to the else if
evaluations, etc., and I'm done!现在我要做的就是格式化一些字体,将其他字母添加到else if
评估等,我就完成了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.