【发布时间】:2021-07-20 15:41:36
【问题描述】:
我是谷歌脚本的新手,通过搜索这个网站学到了很多东西,但现在我想弄清楚如何将单元格名称(A1、A2、.....)添加到使用谷歌脚本的公式。
请在此处查看我的示例:https://docs.google.com/spreadsheets/d/1mttb7dD4RvfXmNt3xGe7cAR6SAXB7Tom97vNOK-YQB0/edit?usp=sharing
在 C 列您会看到“=A16+1”,但它应该类似于“=A2+1”,因为我在第 2 行
这是谷歌脚本代码
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("test1");
var range = sheet.getActiveCell();
var valueToWatch = "y";
var searchRange = sheet.getRange("A1:A" + sheet.getLastRow()); // Modified
// get the values in an array
var values = searchRange.getValues();
// examine the values in the array
var i = [];
for (var y = 0; y < values.length; y++) {
if (values[y][0] == valueToWatch) {
i.push("C" + (y + 1)); // Modified
}
}
//sheet.getRangeList(i).setValue("this is a test");
var data = [];
for (var x = 0; x < values.length; x++) {
data = ['=A' + (x + 1).toString() + '&"1"'];
}
sheet.getRangeList(i).setFormula(data);
}
【问题讨论】:
-
小建议:缩进代码会更容易阅读。
标签: google-apps-script google-sheets