javascript 每个循环的简单谷歌电子表格?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18347921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple Google Spreadsheet For Each Loop?
提问by THYMOLE .
It's been a long time since I've coded. I've never looked into scripts for google spreadsheets before. I just want to make a simple effect to edit the spreadsheet. If I understand correctly, this is doable so long as you run it manually?
自从我编码以来已经很长时间了。我以前从未研究过谷歌电子表格的脚本。我只想做一个简单的效果来编辑电子表格。如果我理解正确,只要您手动运行它就可以做到吗?
The syntax is throwing me off too much. My basic goal is to set each cell in a fixed column range to equal itself plus the value in the adjacent column, and then set that second value to 0.
语法太让我失望了。我的基本目标是将固定列范围中的每个单元格设置为等于自身加上相邻列中的值,然后将第二个值设置为 0。
My instinct would be to do something such as
我的直觉是做一些诸如
CellRange[i][j] selected = C9:D13;
for(i=0,i<selectedrange.length,i++){
SpreadsheetApp.getActiveRange().setValue(selected[i][j]+selected[i][j+1];
SpreadsheetApp.getRange(selected[i][j+1]).setValue(0);
}
That's probably terribly wrong but I feel I ought to at least throw my best guess out before asking for help.
这可能是非常错误的,但我觉得我应该至少在寻求帮助之前抛出我最好的猜测。
回答by THYMOLE .
Say, the goal is to process the range C9:D13 by adding the value in D to the value in C, and then setting D to zero. The code would look like this:
比如说,目标是通过将 D 中的值与 C 中的值相加,然后将 D 设置为零来处理范围 C9:D13。代码如下所示:
function addColumns() {
var sheet = SpreadsheetApp.getActiveSheet(); // or getSheetByName
var range = sheet.getRange("C9:D13");
var values = range.getValues();
for (var i = 0; i < values.length; i++) {
values[i][0] = values[i][0] + values[i][1];
values[i][1] = 0;
}
range.setValues(values);
}
Here, values[i][0]
means values the first column in the range we're working on (namely C) and values[i][1]
refers to the second. The indexing of JavaScript arrays begins with 0, unlike the numbering of rows in spreadsheets.
在这里,values[i][0]
表示我们正在处理的范围内的第一列(即 C)的值,并values[i][1]
引用第二列。JavaScript 数组的索引从 0 开始,这与电子表格中的行编号不同。