简体   繁体   English

Google 表格脚本 - 将粘贴值从一列复制到另一列

[英]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.

相关问题 将一个工作簿中的值粘贴到另一个工作簿底部的脚本-Google表格 - Script to paste values from one workbook into the bottom of another - google sheets 谷歌脚本在编辑时将值从一列复制并粘贴到另一列 - Google Script to copy and paste values from one column to another upon edits 如何使用 Google Sheets Macros 从一张工作表复制值并将它们粘贴到另一张工作表中? - How to copy values from one sheet and paste them into another using Google Sheets Macros? Google表格:根据复选框从一张纸复制并粘贴到另一张纸 - Google Sheets: Copy and Paste from one sheet to another based on checkbox Google Sheets + Apps Scripts,从一个工作表复制/粘贴到另一个工作表,但粘贴到特定列 (B) 中的第一个空单元格 - Google Sheets + Apps Scripts, Copy/Paste from one sheet to another, but Paste into first empty cell in specific column (B) 使用谷歌脚本从一个文件清除、复制和粘贴到另一个文件 - Clear, copy and paste from one file to another using google script 谷歌表格脚本根据匹配列将单元格从一个选项卡复制到另一个选项卡 - Google Sheets Script to Copy Cells From One Tab to Another Based on Matching Column 复制粘贴脚本谷歌表格 - Copy Paste Script Google Sheets Google 工作表脚本将数据集从一张工作表复制到另一张工作表的顶部(仅限值) - Google sheets script copy data sets from one sheet to another (values only) at the top of the sheet 自动将值从一个谷歌工作表复制并粘贴到另一个 - Automatically copy and paste values from one google sheet to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM