簡體   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