Google脚本复制无效的工作表 [英] Google Script to Duplicate Sheet that is Not Active
问题描述
在Google电子表格中,我想使用脚本对工作簿中的现有电子表格进行简单的复制/重命名.我在网上找到的所有示例都是从活动电子表格中复制的.如果不先激活/选择我要复制的电子表格,就不能执行以下任务吗?
In Google spreadsheets, i would like to perform a simple copy/rename of an existing spreadsheet in a workbook using a script. All the examples i have found online are copying from the active spreadsheet. Can you not perform the below task without first activating/selecting the spreadsheet i want to duplicate?
示例: 我的工作簿包含以下工作表名称:橙色",蓝色"
Example: My workbook contains the following sheet names: "Orange", "Blue"
我要复制蓝色"并将其重命名为红色".因此,我现在只剩下3张纸,分别是橙色",蓝色",红色",而将活动电子表格保留在橙色"上.
I want to make a copy of "Blue" and rename it to "Red". So i am left with now 3 sheets "Orange", "Blue", "Red" all while keeping my active spreadsheet on "Orange".
推荐答案
您可以使用 copyTo()方法将工作表复制到同一电子表格中,它会创建一个副本!
You can use the copyTo() method to copy a sheet into the same spreadsheet, it creates a copy!
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheet = source.getSheetByName('Blue');
sheet.copyTo(source).setName('Red');
这篇关于Google脚本复制无效的工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!