
[英]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.