Nic*_*nes 1 google-sheets google-apps-script
我有一个包含多个工作表的谷歌电子表格,我想将每个单独的工作表复制到一个新的电子表格中,并以特定单元格中的文本命名新的电子表格。我很高兴多次运行该脚本,因此我想让它复制活动工作表。
即我所拥有的 = 名为“颜色”的电子表格 - 表 1=“红色”,表 2=“蓝色”,表 3=“黄色”等。
我想要什么 =
名为“Red”的电子表格。电子表格称为“蓝色”,电子表格称为“黄色”
到目前为止,我有这个脚本,但它告诉我“找不到脚本函数: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.
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。
您需要将电子表格作为文件打开,而不是作为电子表格才能使用 makeCopy 函数。
所以你的代码中的这一行不正确:
var sstocopy = ss.getActiveSheet(); // Get spreadsheet with DriveApp.
Run Code Online (Sandbox Code Playgroud)
它应该是:
var sstocopy = DriveApp.getFileById(ss.getId()); // Get spreadsheet with DriveApp.
Run Code Online (Sandbox Code Playgroud)
所以正确的代码如下:
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.
Run Code Online (Sandbox Code Playgroud)
回复您的评论:
为了您的目的,应按以下方式修改代码:
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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6542 次 |
最近记录: |