[英]Google sheets script - copy paste values from one column to another
I have a problem with copy/paste script.我有复制/粘贴脚本的问题。 I have column B (with formulas).我有 B 列(带公式)。 I want to create new column after B, so it will be C and after that copy values from B to column C.我想在 B 之后创建新列,所以它将是 C,然后将值从 B 复制到 C 列。
I have that:我有这个:
function copyData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getActiveSheet();
sh.insertColumnAfter(2);
var range = sh.getRange("B1:B20");
var values = range.getValues();
var add = sh.getRange("C1:C20");
add.setValue(values);
}
After use that script I have new column, but whole column has values only from cell B1 (in 20 rows the same).使用该脚本后,我有了新列,但整列只有来自单元格 B1 的值(20 行相同)。 Can someone tell my why this is happening?有人能告诉我为什么会这样吗?
Change the last line to将最后一行更改为
add.setValues(values);
The var 'values' is a two-dimensional array. var 'values' 是一个二维数组。 If you use setValue() only the first value (top of the range) will be set to the whole target range.如果您使用 setValue() 只有第一个值(范围的顶部)将被设置为整个目标范围。
Reference: .setValues()参考: .setValues()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.