简体   繁体   English

Google脚本可将电子表格中的工作表复制到新电子表格,并以特定单元格命名新电子表格

[英]Google script to copy sheet in spreadsheet to new spreadsheet and name new spreadsheet after specific cell

I have a google spreadsheet with multiple sheets within it, I would like to copy each individual sheet into a new spreadsheet and to have the new spreadsheet named after text in a specific cell. 我有一个包含多个工作表的google电子表格,我想将每个工作表复制到一个新的电子表格中,并让新的电子表格以特定单元格中的文本命名。 I am happy to run the script multiple times so I figured to have it copy the active sheet. 我很高兴多次运行该脚本,因此我想让它复制活动工作表。

ie What I have = Spreadsheet called "Colours" - Sheet 1="red", Sheet 2= "blue", Sheet 3= "yellow", etc. 即我所拥有的=电子表格称为“颜色”-表格1 =“红色”,表格2 =“蓝色”,表格3 =“黄色”,依此类推。

What I want = 我想要的=

Spreadsheet called "Red". 电子表格称为“红色”。 Spreadsheet called "blue", Spreadsheet called "yellow" 电子表格称为“蓝色”,电子表格称为“黄色”

So far I have this script but its telling me "Script function not found: saveAsSpreadsheet For more information" 到目前为止,我有这个脚本,但是它告诉我“找不到脚本功能:saveAsSpreadsheet有关更多信息”

function copyDocument() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); // Get current active spreadsheet.
var sstocopy = ss.getActiveSheet(); // Get spreadsheet with DriveApp.
var sheet = ss.getActiveSheet(); // Get current active sheet.
var sheet_name = sheet.getRange("i2").getValue(); // Get the value of cell B1, used to name the new spreadsheet.
var folder = DriveApp.getFolderById("xxxxxxxxxxxxx"); // Get the ID of the folder where you will place a copy of the spreadsheet.
sstocopy.makeCopy(sheet_name,folder); // Make a copy of the spreadsheet in the destination folder.

Any help would be greatly appreciated. 任何帮助将不胜感激。

You need to open your spreadsheet as a file, not as a spreadsheet to be able to use makeCopy function. 您需要将电子表格作为文件而不是电子表格打开才能使用makeCopy函数。

So this line in your code is not correct: 因此,您代码中的这一行是不正确的:

var sstocopy = ss.getActiveSheet(); // Get spreadsheet with DriveApp.

It should be: 它应该是:

var sstocopy = DriveApp.getFileById(ss.getId()); // Get spreadsheet with DriveApp.

So the correct code is the following: 因此,正确的代码如下:

function copyDocument() {
    var ss = SpreadsheetApp.getActiveSpreadsheet(); // Get current active spreadsheet.
    var sstocopy = DriveApp.getFileById(ss.getId()); // Get spreadsheet with DriveApp.
    var sheet = ss.getActiveSheet(); // Get current active sheet.
    var sheet_name = sheet.getRange("i2").getValue(); // Get the value of cell B1, used to name the new spreadsheet.
    var folder = DriveApp.getFolderById("xxxxxxxxxxxxx"); // Get the ID of the folder where you will place a copy of the spreadsheet.
    sstocopy.makeCopy(sheet_name,folder); // Make a copy of the spreadsheet in the destination folder.

Answer to your comment: 回答您的评论:

For your purpose the code should be modified in this way: 为了您的目的,应按以下方式修改代码:

var sheet = SpreadsheetApp.getActiveSheet(); // Get current active sheet.
var sheet_name = sheet.getRange("i2").getValue(); // Get the value of cell B1, used to name the new spreadsheet.

var folder = DriveApp.getFolderById("xxxxxxxxxxxxx"); // Get the ID of the folder where you will place a copy of the spreadsheet.

var newSS = SpreadsheetApp.create(sheet_name); // create new blank spreadsheet in a root folder
var asFile = DriveApp.getFileById(newSS.getId()); // get new spreadsheet as a file

folder.addFile(asFile); // add this file to destination folder
DriveApp.getRootFolder().removeFile(asFile); // remove a file from root folder

var copiedSheet = sheet.copyTo(newSS); // copy active sheet to new spreadsheet
copiedSheet.setName(sheet_name); // rename copied sheet
newSS.deleteSheet(newSS.getSheetByName('Sheet1')); // remove "Sheet1" sheet which was created by default in new spreadsheet

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将工作表复制到新电子表格,将单元格值添加到新电子表格名称的末尾 - Copy sheet to new spreadsheet, Add cell value to end of new spreadsheet name Google Sheet(脚本)将Sheet复制到另一个SpreadSheet并在目标SS中命名新工作表 - Google Sheet (Script) copy Sheet to another SpreadSheet and name new sheet in target SS 将工作表复制为新电子表格的脚本无法正常工作 - Script to copy sheet as a new spreadsheet not working well 使用谷歌工作表脚本时,如何将电子表格的工作表复制到另一个电子表格中而不创建新的工作表副本? - How to copy spreadsheet's sheet into another spreadsheet without creating a new sheet copy when using google sheet script? Google脚本-将动态范围复制到新的电子表格 - Google Script - Copy dynamic range to new spreadsheet Google Spreadsheet,使用工作表中的单元格检查工作表的名称 - Google Spreadsheet, Checking the name of the sheet with a cell in a sheet 将工作表中的值和格式复制到新的Google电子表格文档中? - Copy value and format from a sheet to a new google Spreadsheet document? 创建新的电子表格,以表格 Google Scripts 的名称命名 - Create new spreadsheet name it by the name of sheet Google Scripts 将模板电子表格复制到文件夹,然后写入新副本(Google脚本) - Copy template spreadsheet to folder, then write to the new copy (google script) 将工作表复制到另一个电子表格[Google Apps脚本] - Copy sheet to another spreadsheet [Google Apps Script]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM