简单的Google Spreadsheet for Each Loop? [英] Simple Google Spreadsheet For Each Loop?

查看:105
本文介绍了简单的Google Spreadsheet for Each Loop?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我编写代码已经很长时间了。我从来没有看过Google电子表格的脚本。我只想做一个简单的效果来编辑电子表格。如果我理解正确,只要你手动运行它,这是可行的?



语法太让我失望了。我的基本目标是将每个单元格设置在一个固定的列范围内,使其等于自身加上相邻列中的值,然后将该第二个值设置为0. 我的本能可以做一些事情,比如

  CellRange [i] [j] selected = C9:D13; 
(i = 0,i< selectedrange.length,i ++){
SpreadsheetApp.getActiveRange()。setValue(selected [i] [j] + selected [i] [j + 1];
SpreadsheetApp.getRange(selected [i] [j + 1])。setValue(0);
}

这可能是非常错误的,但我觉得在寻求帮助之前,我应该至少抛出我最好的猜测。

解决方案

假设目标是通过将D中的值加上C中的值来处理范围C9:D13,然后将D设置为零。代码如下所示:

  function addColumns(){
var sheet = SpreadsheetApp.getActiveSheet(); //或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);

这里, values [i] [0] 表示val (即C)和 values [i] [1] 指第二列。 JavaScript数组的索引从0开始,而不像电子表格中的行数。


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.

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.

解决方案

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:

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.

这篇关于简单的Google Spreadsheet for Each Loop?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆