我正在尝试创建一个脚本,在我的Google云端硬盘中创建一个新的电子表格,然后将特定Google云端硬盘文件夹中的工作表合并到新创建的电子表格中。
截至目前,我收到以下错误:无法将[object Object]转换为Spreadsheet。 (第34行,文件“代码”)
我复制了以下代码。
function mergeSheets() {
/* Retrieve the desired folder */
var myFolder = DriveApp.getFoldersByName("Merging Spreadsheet Test Folder").next();
/* Get all spreadsheets that resided on that folder */
var spreadSheets = myFolder.getFilesByType("application/vnd.google-apps.spreadsheet");
var newSpreadSheet = {
"title": "Test Merge Document",
"mimeType": "application/vnd.google-apps.document",
"parents": [
{
"id": "0B4DgXVz2cL8oRURkdUNCZEhITG8"
}
]
};
Drive.Files.insert(newSpreadSheet);
/* Iterate over the spreadsheets over the folder */
while(spreadSheets.hasNext()) {
var sheet = spreadSheets.next();
/* Open the spreadsheet */
var spreadSheet = SpreadsheetApp.openById(sheet.getId());
/* Get all its sheets */
for(var y in spreadSheet.getSheets()) {
/* Copy the sheet to the new merged Spread Sheet */
spreadSheet.getSheets()[y].copyTo(newSpreadSheet);
}
}
}
非常感谢任何帮助。
答案 0 :(得分:0)
您正在尝试将工作表保存到对象而不是电子表格。有两种方法可以解决这个问题。
您可以使用以下内容创建电子表格:
var newSpreadSheet = SpreadsheetApp.create("Test Merge Document");
或在您制作后打开工作表:
var newSpreadSheet = {
"title": "Test Merge Document",
"mimeType": "application/vnd.google-apps.document",
"parents": [
{
"id": "0B4DgXVz2cL8oRURkdUNCZEhITG8"
}
]
};
var newSS = Drive.Files.insert(newSpreadSheet),
newSpreadSheet = SpreadsheetApp.openById(newSS.id);
答案 1 :(得分:0)
这是我最终使用的解决方案。像魅力一样。
function mergeSheets() {
/* Retrieve the desired folder */
var myFolder = DriveApp.getFoldersByName("Enter Folder Name Here").next();
/* Get all spreadsheets that resided on that folder */
var spreadSheets = myFolder.getFilesByType("application/vnd.google-apps.spreadsheet");
/* Creates new spreadsheet to merge documents into */
var newSpreadSheet = SpreadsheetApp.create("Enter Name For New Spreadsheet Here");
/* Iterate over the spreadsheets over the folder */
while(spreadSheets.hasNext()) {
var sheet = spreadSheets.next();
/* Grabs name of the document */
var ssName = sheet.getName();
Logger.log(ssName)
/* Open the spreadsheet */
var spreadSheet = SpreadsheetApp.openById(sheet.getId());
/* Get all its sheets */
for(var y in spreadSheet.getSheets()) {
/* Copy the sheet to the new merged Spread Sheet */
spreadSheet.getSheets()[y].copyTo(newSpreadSheet).setName(ssName);
}
}
}