簡體   English   中英

您無權調用 openById

[英]You do not have permission to call openById

問題:當我運行腳本時,谷歌告訴我,

您無權調用 openById

我從另一個 Google 電子表格復制了一個腳本,並更改​​了target_ssKey變量的單元格引用,並在源和目標電子表格中創建了大小合適的命名范圍。

Google Apps Script 文檔沒有說明它可能不起作用的原因:

https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app#openById%28String%29

work for me because I invoke it from a custom menu:另一個 Google Apps Script 文檔說它對我有用,因為我從自定義菜單調用它:

https://developers.google.com/apps-script/guides/sheets/functions#using_apps_script_services

上面的第二個鏈接說:

如果您的自定義函數拋出錯誤消息 ,該服務需要用戶授權,因此不能在自定義函數中使用。

要使用上面列出的服務以外的服務,請創建一個運行 Apps 腳本函數的自定義菜單,而不是編寫自定義函數。 從菜單觸發的功能將在必要時要求用戶授權,因此可以使用所有 Apps 腳本服務。

我嘗試將該函數放入“自定義函數”項目,然后放入“附加”項目,但仍然收到相同的錯誤消息。

關於我做錯了什么以及如何使這項工作有任何想法?

這是我的確切代碼:

function exportData_SStoSS() {
    //  Get the source data.
    var source_ss = SpreadsheetApp.getActiveSpreadsheet();
    var data = source_ss.getRangeByName("exportData").getValues();

    //  Identify the target.
    var controls_sh = source_ss.getSheetByName("Controls");
    var target_ssKey = controls_sh.getRange('C2').getValue();
    var target_ss = SpreadsheetApp.openById(target_ssKey);

    //  Paste the data to the target.
    target_ss.getRangeByName("importData").setValues(data);
};

我想我會拋出一個類似的問題,我遇到了這個問題,在那里我收到了錯誤You don't have permission to call by openById

在我的例子中,我試圖從我從這個例子中復制的translate.gs調用函數:

https://developers.google.com/apps-script/quickstart/docs

請注意,在translate.gs的頂部

/**
 * @OnlyCurrentDoc
 *
 * The above comment directs Apps Script to limit the scope of file
 * access for this add-on. It specifies that this add-on will only
 * attempt to read or modify the files in which the add-on is used,
 * and not all of the user's files. The authorization request message
 * presented to users will reflect this limited scope.
 */

這里的罪魁禍首是@OnlyCurrentDoc評論。 請參閱此處以供參考:

https://developers.google.com/apps-script/guides/services/authorization

刪除@OnlyCurrentDoc為我解決了這個問題

我找到了這個官方說明,我相信它澄清了導致問題的原因。

如果您的函數是自定義函數,即可以像工作表本身中的常規電子表格函數一樣使用的函數,那么它對事物的訪問權限有限,無法打開其他電子表格。

然而,相同的腳本可以從菜單按鈕或類似按鈕打開其他電子表格。

鏈接: developers.google.com 上的文檔

我可以使用谷歌開發人員的這個自動化指南解決這個問題。

https://developers.google.com/apps-script/concepts/scopes#setting_explicit_scopes

此條目在 json 文件中是必需的。

 "oauthScopes": [
      "https://www.googleapis.com/auth/spreadsheets.readonly",
      "https://www.googleapis.com/auth/userinfo.email",
      "https://www.googleapis.com/auth/spreadsheets"
  ],

方法openById可以從“空白項目”調用,但不能從“表格中的自定義函數”或“Google 表格openById ”項目調用。

我以為“空白項目”會創建一個與我的電子表格無關的項目,但我錯了。 空白項目連接到我的電子表格。 我嘗試使用的其他類型的項目似乎是腳本項目的范圍有限版本,無法執行某些 GAS 方法。

有同樣的問題,並來分享我的解決方案。 就我而言,我有兩個電子表格,分別稱為 A 和 B。兩者都使用綁定到每個電子表格的腳本。 電子表格 B 能夠將數據寫入電子表格 A 的選項卡。但電子表格 A 在嘗試從電子表格 B 讀取時不斷收到“您無權調用 openById”錯誤。然后我嘗試將其添加為自定義菜單項,但還是同樣的問題。

事實證明,我的解決方案非常簡單。 我在 script.google.com 中創建了一個新的未綁定腳本,它使用 openById 調用兩個電子表格。 第一次跑步時,我的臉上帶着微笑,因為它要求授權。 此后一帆風順。

問題:運行腳本時,Google告訴我,

您無權調用openById

我從另一個Google電子表格中復制了一個腳本,並更改​​了target_ssKey變量的單元格引用,並在Source和Target電子表格中創建了大小適當的Named Ranges。

Google Apps腳本文檔對可能無法正常工作的原因一無所知:

https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app#openById%28String%29

work for me because I invoke it from a custom menu:另一個Google Apps腳本文檔說它對我有用,因為我是從自定義菜單調用它的:

https://developers.google.com/apps-script/guides/sheets/functions#using_apps_script_services

上面的第二個鏈接說:

如果您的自定義函數拋出錯誤消息,則 ,該服務需要用戶授權,因此無法在自定義功能中使用。

要使用上面列出的服務以外的服務,請創建一個自定義菜單,該菜單運行一個Apps Script函數,而不是編寫一個自定義函數。 通過菜單觸發的功能將在必要時詢問用戶授權,因此可以使用所有Apps Script服務。

我嘗試將函數放入“自定義函數”項目中,然后放入“附加”項目中,但仍然收到相同的錯誤消息。

關於我做錯了什么以及如何進行這項工作的任何想法?

這是我的確切代碼:

function exportData_SStoSS() {
    //  Get the source data.
    var source_ss = SpreadsheetApp.getActiveSpreadsheet();
    var data = source_ss.getRangeByName("exportData").getValues();

    //  Identify the target.
    var controls_sh = source_ss.getSheetByName("Controls");
    var target_ssKey = controls_sh.getRange('C2').getValue();
    var target_ss = SpreadsheetApp.openById(target_ssKey);

    //  Paste the data to the target.
    target_ss.getRangeByName("importData").setValues(data);
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM