我有一个脚本,可以创建一个新的电子表格并将两个选项卡从当前工作表复制到新工作表。我希望将新工作表保存在共享云端硬盘文件夹中,而不是我的云端硬盘中。以下是我正在进行的当前迭代。注释掉的行是不起作用的。
function SaveNewFinancials() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Inputs'), true);
var filename = spreadsheet.getRange("B15").getValue();
var newSpreadsheet = SpreadsheetApp.create(filename);
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Balance Sheet'), true);
SpreadsheetApp.getActiveSheet().copyTo(newSpreadsheet).setName("Balance Sheet");
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Profit and Loss'), true);
SpreadsheetApp.getActiveSheet().copyTo(newSpreadsheet).setName("Profit and Loss");
newSpreadsheet.deleteSheet(newSpreadsheet.getSheetByName("Sheet1"));
//var folderID = "xxxxxxx3nTzzyS-I1pWzsejHDCh5";
//var sharedFolder = DriveApp.getFolderById(folderID);
//var sharedDriveID = "xxxxxxxxenHXUk9PVA";
//var resource = {title: filename, mimeType: MimeType.GOOGLE_SHEETS, parents: [{driveid: sharedDriveID, id: sharedFolder}]};
//Drive.Files.copy(resource, newSpreadsheet,{supportsAllDrives: true});
我已启用高级驱动器服务,但尝试在共享驱动器中查找文件夹时仍然出现错误。
我尝试过使用 moveTo 和 copyTo,但我了解到 DriveApp 不能很好地与共享云端硬盘配合使用。我也不认为我需要驱动器 ID,但我在这里的其他几个答案中看到了它,所以我在最近的尝试中添加了它。预先感谢您的帮助!
尝试:
function SaveNewFinancials() {
var spreadsheet = SpreadsheetApp.getActive();
// Set the active sheet to 'Inputs' and get the filename from cell B15
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Inputs'), true);
var filename = spreadsheet.getRange("B15").getValue();
// Create a new spreadsheet with the obtained filename
var newSpreadsheet = SpreadsheetApp.create(filename);
// Copy the 'Balance Sheet' sheet to the new spreadsheet and rename it
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Balance Sheet'), true);
SpreadsheetApp.getActiveSheet().copyTo(newSpreadsheet).setName("Balance Sheet");
// Copy the 'Profit and Loss' sheet to the new spreadsheet and rename it
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Profit and Loss'), true);
SpreadsheetApp.getActiveSheet().copyTo(newSpreadsheet).setName("Profit and Loss");
// Delete the default 'Sheet1' from the new spreadsheet
newSpreadsheet.deleteSheet(newSpreadsheet.getSheetByName("Sheet1"));
// The shared folder ID where the new spreadsheet will be moved
var folderID = "xxxxxxx3nTzzyS-I1pWzsejHDCh5"; // Replace with your shared folder ID
var fileID = newSpreadsheet.getId();
// Use the Drive API to move the file to the shared folder
var file = DriveApp.getFileById(fileID);
var folder = DriveApp.getFolderById(folderID);
folder.addFile(file);
DriveApp.getRootFolder().removeFile(file);
}
作为另一种方法,以下修改如何?
关于
but I still get errors when attempting to find the folder in the shared drive.
,不幸的是,从你的问题来看,我无法确认你的实际错误信息。但是,根据您的显示脚本和脚本编辑器的当前规范,您可能已经通过启用 Drive API v3 测试了您的显示脚本。现阶段,当在高级 Google 服务中启用 Drive API 时,版本 3 会自动设置为默认版本。如果是这样,则会发生错误,因为显示脚本的 resource
适用于 Drive API v2。如果我的理解是正确的,那么以下示例脚本怎么样?
在测试之前,请在高级 Google 服务中启用 Drive API v3。请将您的目标文件夹 ID 设置为 destinationFolderId
。
/**
* ### Description
* Check whether Drive API is enabled at Advanced Google services, and return it as true or false and the version.
* ref: https://medium.com/google-cloud/checking-api-enabled-with-advanced-google-services-using-google-apps-script-572bcdeb39a8
*
* @param {String} apiName API name you want to check.
* @returns {Object} Object including "api" and "version" properties.
*/
function isAPIAtAdvancedGoogleServices_(apiName) {
if (!apiName || apiName == "" || typeof apiName != "string") {
throw new Error("Please set a valid API name.");
} else if (!/^[A-Z]+$/g.test(apiName[0])) {
const [t, ...b] = apiName;
apiName = [t.toUpperCase(), ...b].join("");
}
const obj = { apiName, api: "disable" };
if (typeof this[apiName] !== "undefined") {
obj.api = "enable";
obj.version = this[apiName].getVersion();
}
return obj;
}
function SaveNewFinancials() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Inputs'), true);
var filename = spreadsheet.getRange("B15").getValue();
var newSpreadsheet = SpreadsheetApp.create(filename);
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Balance Sheet'), true);
SpreadsheetApp.getActiveSheet().copyTo(newSpreadsheet).setName("Balance Sheet");
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Profit and Loss'), true);
SpreadsheetApp.getActiveSheet().copyTo(newSpreadsheet).setName("Profit and Loss");
newSpreadsheet.deleteSheet(newSpreadsheet.getSheetByName("Sheet1"));
// I modified the below script.
const destinationFolderId = "###"; // Please set your destination folder ID (in shared drive).
const sourceFileId = newSpreadsheet.getId();
const check = isAPIAtAdvancedGoogleServices_("Drive");
if (check.api == "enable" && check.version == "v3") {
SpreadsheetApp.flush(); // I'm not sure whether this line is required.
Drive.Files.update({}, sourceFileId, null, { addParents: destinationFolderId, enforceSingleParent: true, supportsAllDrives: true });
} else {
console.warn("Please enable Drive API v3 at Advanced Google services.");
}
}
使用云端硬盘服务(DriveApp)I've tried using moveTo and copyTo, but I learned that DriveApp doesn't play well with shared drives.
,当我测试以下用于将电子表格移动到共享驱动器的脚本时,我确认可以将电子表格从我的驱动器移动到共享驱动器。所以,请测试以下脚本。请将您的目标文件夹 ID 设置为
destinationFolderId
。
function SaveNewFinancials() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Inputs'), true);
var filename = spreadsheet.getRange("B15").getValue();
var newSpreadsheet = SpreadsheetApp.create(filename);
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Balance Sheet'), true);
SpreadsheetApp.getActiveSheet().copyTo(newSpreadsheet).setName("Balance Sheet");
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Profit and Loss'), true);
SpreadsheetApp.getActiveSheet().copyTo(newSpreadsheet).setName("Profit and Loss");
newSpreadsheet.deleteSheet(newSpreadsheet.getSheetByName("Sheet1"));
// I modified the below script.
const destinationFolderId = "###"; // Please set your destination folder ID (in shared drive).
const sourceFileId = newSpreadsheet.getId();
SpreadsheetApp.flush(); // I'm not sure whether this line is required.
DriveApp.getFileById(sourceFileId).moveTo(DriveApp.getFolderById(destinationFolderId));
}
注: