我一直在使用一個簡單的 Google Apps 腳本代碼將檔案從我的 Google Drive 下載到我的本地計算機。
var dlUrl = "https://drive.google.com/uc?export=download&id=" file.getId();
// Open a dialog and run Javascript for downloading the file.
var str = '<script>window.location.href="' dlUrl '"</script>';
var html = HtmlService.createHtmlOutput(str);
SpreadsheetApp.getUi().showModalDialog(html, "Downloading invoice ... please wait");
// This is used for closing the dialog.
Utilities.sleep(3000);
file.setTrashed(true);
var closeHtml = HtmlService.createHtmlOutput("<script>google.script.host.close()</script>");
SpreadsheetApp.getUi().showModalDialog(closeHtml, "Downloading invoice ... please wait");
這段代碼已經作業了一年多,然后幾天前突然停止作業,代碼沒有改變。當我運行它時,我現在收到訊息:“403。這是一個錯誤。很抱歉,您無權訪問此頁面。這就是我們所知道的。”
我知道該檔案存在并且可以訪問,因為如果我將 dlUrl 中的字串放入瀏覽器的地址欄中,則檔案下載不會出現問題。
我也有:
- 已退出所有其他 Google 帳戶,并且僅登錄到擁有該檔案的帳戶。
- 洗掉了帳戶的所有已保存密碼
- 試圖將檔案權限更改為通用編輯權限
雖然我確實在修改 Google Apps Script,但我認為自己仍然是初學者,并希望就我應該嘗試的內容提出任何建議。非常感謝您的幫助。
uj5u.com熱心網友回復:
在這種情況下,請進行如下修改。
從:
var str = '<script>window.location.href="' dlUrl '"</script>';
到:
var str = '<script>window.open("' dlUrl '", "_blank")</script>';
window.location.href是 的屬性window。Refwindow.open是 的一種方法window。Refwindow.open可以使用_blank,window.location.href不能使用。即,window.open可以將 URL 作為新視窗打開。我想這可能是造成這種情況的原因。- 并且,當
_self用作 的目標時window.open,403. That’s an error. We're sorry, but you do not have access to this page. That’s all we know.會發生與您相同的錯誤。而且,當_top和_parent使用,不會出現下載。所以我建議使用_blank.
參考:
- 視窗位置
- 視窗.open()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/376107.html
標籤:谷歌应用程序脚本
