创建新工作表并在 Google 文档中的电子表格之间移动工作表

I-M*_*-JM 0 google-docs google-sheets google-docs-api google-sheets-api

我有两个要求:

1)我想以编程方式从当前电子表格中的现有工作表创建一个新工作表

2) 我想以编程方式将一张工作表从一个电子表格复制到另一个电子表格

帮助表示赞赏。

Hen*_*reu 5

由于您尚未指定环境/语言。我认为最简单的方法是使用Apps 脚本,因为它内置在 Google 电子表格中。

下面是执行此操作的示例代码:

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('SheetName');
  sheet.copyTo(ss).setName('NewName'); //copy to the same spreadsheet.
  //get a different spreadsheet (you can get its id in the url)
  var os = SpreadsheetApp.openById('any-spreadsheet-key-that-you-can-edit');
  //copy sourceSheet from one spreadsheet to another
  sheet.copyTo(os).setName('AnotherName');
}
Run Code Online (Sandbox Code Playgroud)

要运行它,只需打开包含要复制的工作表的电子表格,单击菜单“工具”>“脚本编辑器”。粘贴代码,“保存并运行”> myFunction