I'm having problems calling a server-side Apps Script function from a html sidebar in Google Sheets.
I've replicated my issue with the simple example code below.
Clicking the button should call alert
in my Code.gs script and display an alert to the user.
Clicking the button shows the error:
We're sorry, a server error occurred while reading from storage. Error code PERMISSION_DENIED.
There are no entries in the 'executions' section of the Apps Script dashboard for alert()
, so it seems like the function never even gets called?
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('Matthew')
.addItem('Show Sidebar', 'sidebar')
.addToUi();
};
function sidebar() {
const html = HtmlService.createHtmlOutputFromFile('test.html')
.setTitle('Matthew Experiment')
.setWidth(300);
SpreadsheetApp.getUi().showSidebar(html);
}
function alert() {
SpreadsheetApp.getUi().alert("alert!");
}
<body>
<div class="container">
<button class="btn btn-success" onclick="doSomething()">Click Me!</button>
</div>
<script>
function doSomething() {
google.script.run.withFailureHandler(function(error) {
console.log("error")
console.log(error.message)
}).withSuccessHandler(function(result) {
console.log("done!")
}).alert();
}
</script>
</body>
I've confirmed that the client-code is getting properly inflated with server-side function names, it's just calling the function that doesn't work.
My code seems to be the same as all of the getting started guides, but I don't see anyone else posting about this issue. Hoping someone here has deeper knowledge of the problem than I do.
I opened a private window and it worked, so I thought, maybe this is an extension causing problems? But even disabling all extensions doesn't fix it in the main window. I've logged out/in to my google account but still can't get it to work without opening a private browser window.
This problem is consistent across chrome/edge/firefox.
This happens when the global logged in user is different from the owner of script.
Logout all your google account and then re-login with the account you used to created the script. This fixed the issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With