问题
I try to implement a sidebar on my spreadsheet to get user input for my scripts to use. I haven't been able to get it to successfully call any server side functions. I put together a simple script from the google documentation and several stackoverflow questions that I read through, but I keep getting an error. It is able to print to the console, but it errors out trying to call the logText() function with google.script.run.
Script File:
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Extra Functions')
.addItem('Show sidebar', 'showSidebar')
.addToUi();
}
function showSidebar() {
var html = HtmlService.createHtmlOutputFromFile('Test')
.setTitle('Testing')
.setWidth(300);
SpreadsheetApp.getUi()
.showSidebar(html);
}
function logInput(text) {
Logger.log(text);
}
HTML File (Test.html):
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function onFailure(error) {
var div = document.getElementById('output');
div.innerHTML = "ERROR: " + error.name + ": " + error.message;
}
function logText(){
var txt = document.getElementById("txt_input").value;
console.log(txt);
google.script.run.withFailureHandler(onFailure).logInput(txt);
}
</script>
</head>
<body>
<label for="txt_input">Input Text:</label>
<input type="text" id="txt_input"><br>
<button onclick='logText()'>Send Name</button><br>
<div id="output"></div>
</body>
</html>
I've tried running it both on the new Apps Script V8 and Apps Script Legacy, and I get a slightly different error on each.
Apps Script Legacy
ERROR: ScriptError: You do not have access to perform that action. Please ask the owner of this item to grant access to you.
Apps Script V8
ERROR: ScriptError: We're sorry, a server error occurred while reading from storage. Error code PERMISSION_DENIED.
I've been doing research on Authorization but as far as I can tell, it has all the permissions it needs as a Container-Bound Script (https://developers.google.com/apps-script/guides/bound). It has the /auth/script.container.ui OAuth Scope which should allow it to "Display and run third-party web content in prompts and sidebars inside Google applications", as well as the /auth/spreadsheets Scope. I am also the owner of the spreadsheet and the script project.
Since it is not functioning as a Web App it does not need to be deployed, and does not need a doGet() function. https://developers.google.com/apps-script/guides/html#serve_html_as_a_google_docs_sheets_slides_or_forms_user_interface
回答1:
I've had the same issue, for me the problem was having a user being logged in with multiple google accounts. It might apply to your case as well. The user tried logging-in with just one account and the permission issue was gone.
It's the same problem that might occur when installing a custom addon. Hope it helps.
回答2:
Seems like a bug in the new engine. I have a similar problem. A function from the script is invoked from the HTML. The new engine fails. I disabled the V8 engine and it worked so it seems to be something internal to Google.
回答3:
I experienced exactly the same problem and solved it disableling the new V8 engine.
来源:https://stackoverflow.com/questions/60442915/container-bound-script-getting-permission-errors-trying-to-run-functions-with-go