繁体   English   中英

服务电子表格在访问 ID 为 xxx 的文档时失败

[英]Service Spreadsheets failed while accessing document with ID xxx

是什么导致了错误

“异常:服务电子表格在访问具有 ID 的文档时失败...”

在我的 Google Apps 脚本代码中?

如何修复此错误并使用 Google Apps 脚本将 Excel 工作表(目前该工作表位于 Google 驱动器上,我可以在运行脚本时从我的硬盘驱动器加载它)成功导入现有的 Google 工作表吗?

是否需要设置特定的配置或权限才能使代码从 Google Drive 访问和读取 Excel 文件?

我对 Google Apps Script 不是很熟悉,欢迎提出任何建议。

这是我的脚本

function importExcelSheet() {
// Ask which Excel file to load
var file = DriveApp.getFilesByName(Browser.inputBox("Enter the name of the Excel file to load:")).next();

// Read the Excel file
var spreadsheet = SpreadsheetApp.open(file);

// Get the first sheet from the Excel file
var sheet = spreadsheet.getSheets()[0];

// Import the sheet into the active Google Sheet
var currentSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var newSheet = currentSpreadsheet.insertSheet(sheet, 0);

// Set the name of the new sheet in the format "YYYY.MMM"
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth();
var monthName = Utilities.formatDate(date, "GMT", "MMM");
newSheet.setName(year + "." + monthName);
}

// Add the function to the Custom Google Sheet menu
function onOpen() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [{name: "Import Excel Sheet", functionName: "importExcelSheet"}];
spreadsheet.addMenu("Custom Menu", menuEntries);
}
I added "Drive API" to the Apps Script from "Services" menu. 

这应该可行(尚未测试)但这并不完美,因为创建了源文件的副本,以便使用 SpreadsheetApp 读取它

并且需要激活 Advanced Drive API: https://developers.google.com/apps-script/advanced/drive


// your code:
// Ask which Excel file to load
var file = DriveApp.getFilesByName(Browser.inputBox("Enter the name of the Excel file to load:")).next();

//then: 
if (file.getContentType() == "application/vnd.ms-excel" || file.getContentType() == "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {

   var id = Drive.Files.insert({title: "new", mimeType: MimeType.GOOGLE_SHEETS}, file.copyBlob()).id;
   var ss = SpreadsheetApp.openById(id);

   //back to your code
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM