简体   繁体   English

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

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

What is causing the error是什么导致了错误

"Exception: Service Spreadsheets failed while accessing document with ID..." “异常:服务电子表格在访问具有 ID 的文档时失败...”

in my Google Apps Script code?在我的 Google Apps 脚本代码中?

How can I fix this error and successfully import an Excel sheet (at the moment that sheet is located on Google drive, can I load it from my hard drive, when I run the script) into existing Google Sheet using Google Apps Script?如何修复此错误并使用 Google Apps 脚本将 Excel 工作表(目前该工作表位于 Google 驱动器上,我可以在运行脚本时从我的硬盘驱动器加载它)成功导入现有的 Google 工作表吗?

Is there a specific configuration or permission that needs to be set in order for the code to access and read an Excel file from Google Drive?是否需要设置特定的配置或权限才能使代码从 Google Drive 访问和读取 Excel 文件?

I am not very familiar with Google Apps Script and any advices are welcomed.我对 Google Apps Script 不是很熟悉,欢迎提出任何建议。

Here is my 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. 

This should work (haven't tested it) but this is not perfect because creating a copy of your source file, in order to read it with SpreadsheetApp这应该可行(尚未测试)但这并不完美,因为创建了源文件的副本,以便使用 SpreadsheetApp 读取它

And you need to activate Advanced Drive API: https://developers.google.com/apps-script/advanced/drive并且需要激活 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