迭代超出范围,将字符串追加到每个范围 [英] Iterate over range, append string to each

查看:118
本文介绍了迭代超出范围,将字符串追加到每个范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google Spreadsheet(活动范围)中选择了一系列单元格。



我想遍历该范围内的每个单元格,然后将字符串添加到结束。字符串总是相同的,并且可以硬编码到函数中。



这看起来很简单,但我一直在弄乱代码一个小时现在不能得到任何有用的事情发生,而文档真的没有帮助。



这是我现在拥有的。我不编码JS(我知道VBA,所有帮助..)。

$ p $ 函数appendString(){
var range = SpreadsheetApp.getActiveSheet()。getActiveRange();
for(var i = 0; i< range.length; i ++){
var currentValue = range [i] .getValue();
var withString = currentValue +string;
range [i] .setValue(withString);
}
}

任何帮助都将非常感谢。

解决方案

您可以尝试如下所示:

  function appendString(){
var range = SpreadsheetApp.getActiveSheet()。getActiveRange();
var numRows = range.getNumRows();
var numCols = range.getNumColumns(); (var i = 1; j< = numCols; j ++){
var currentValue = range.getCell (I,J).getValue();
var withString = currentValue +string;
range.getCell(i,j).setValue(withString);
}
}
}


I have a range of cells selected in a Google Spreadsheet (activerange).

I want to iterate over every cell in that range, and add a string to the end. The string is always the same, and can be hardcoded into the function.

It seems like a really simple thing, but I've been messing with the code for an hour now and can't get anything useful to happen, and the docs are really not helping.

Here's what I have now. I don't code JS (I do know VBA, for all that helps..).

function appendString() {
  var range = SpreadsheetApp.getActiveSheet().getActiveRange();
  for (var i = 0; i < range.length; i++) {
    var currentValue = range[i].getValue();
    var withString = currentValue + " string";
    range[i].setValue(withString);
  }
}

Any help would be most appreciated.

解决方案

You can try something like this:

function appendString() {
  var range = SpreadsheetApp.getActiveSheet().getActiveRange();
  var numRows = range.getNumRows();
  var numCols = range.getNumColumns();
  for (var i = 1; i <= numRows; i++) {
    for (var j = 1; j <= numCols; j++) {
      var currentValue = range.getCell(i,j).getValue();
      var withString = currentValue + " string";
      range.getCell(i,j).setValue(withString);
    }
  }
}

这篇关于迭代超出范围,将字符串追加到每个范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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