Google表格脚本:将范围复制到下一个空列

huangapple go评论62阅读模式
英文:

Google Table Script: Copy Range to next free column

问题

我正在开始使用Google Tables的脚本。首先,我想编写一个简单的复制脚本,该脚本将从A列中获取值并将其复制到下一个空列。我目前的问题是copyTo函数不适用于数字值,但使用下一个空列也不适用于B1范围或类似的情况。

我的第二个想法是使用一个将数字范围转换为“普通”范围的函数,类似于convertToRange(2,1),它将返回B1。但我找不到类似的东西。

有人可以帮助我吗?

function copy() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var source = ss.getRange("A2:A10");
  var destColumn = ss.getLastColumn();

  source.copyTo(destColumn, 1);
  source.clear();
}
英文:

I'm getting started on using script with Google Tables. To start I wanted to write a simple copy script which takes the values from column A and copy's it to the next free column. My problem right now is that the copyTo function doesn't work with numerical values, but the whole use next free column doesn't work with B1 Ranges or sth similar.
My second thought was using a function which converts numerical range to a "normal" range, sth like convertToRange(2,1) which would return B1. But I couldn't find anything similar again.

Can anyone help me out here?

function copy () {
  var ss = SpreadsheetApp.getActiveSpreadsheet ();
  var source = ss.getRange ("A2:A10");
  var destColumn = ss.getLastColumn();

  source.copyTo(destColumn,1)
  source.clear ();
}

答案1

得分: 1

copyTo 期望一个范围对象(可以使用行、列坐标或使用 getRange() 方法的A1记法来获得)。

此外,最好也提供您要操作的工作表的名称。

最后,您需要将“最后列”索引增加1,以找到第一个空列。看看这是否有效

function copy () {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  var source = ss.getRange("A2:A10");
  var destColumn = ss.getLastColumn();
  source.copyTo(ss.getRange(1, destColumn+1))
  source.clear();
}

还请参考:
getRange
copyTo.

英文:

copyTo expects a range-object (which can be obtained with row, column coordinates or with A1-Notation with the getRange() method).

Also, it may be better to provide also the name of the sheet you want to work on.

Finally, you will need to increment the "last column" index with 1 to find the first free column. See if this works

  function copy () {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  var source = ss.getRange ("A2:A10");
  var destColumn = ss.getLastColumn();
  source.copyTo(ss.getRange(1, destColumn+1))
  source.clear ();
}

Also see:
getRange and
copyTo.

huangapple
  • 本文由 发表于 2020年1月6日 18:18:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/59610287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定