I am trying to create a simple addon in Google Docs, but after reading the documentation I cannot figure out how it is possible that google.script.run doesn't work properly. Here is my base code:
code.gs
function onOpen(e) {
DocumentApp.getUi().createAddonMenu()
.addItem('Start', 'showSidebar')
.addToUi();
}
function onInstall(e) {
onOpen(e);
}
function showSidebar() {
var ui = HtmlService.createHtmlOutputFromFile('sidebar')
.setTitle('NeuralText');
DocumentApp.getUi().showSidebar(ui);
}
function dummyFunc(){
var doc_id = DocumentApp.getActiveDocument().getId();
Logger.log(doc_id);
return doc_id;
}
sidebar.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<!-- The CSS package above applies Google styling to buttons and other elements. -->
<style>
.branding-below {
bottom: 56px;
top: 0;
}
.branding-text {
left: 7px;
position: relative;
top: 3px;
}
.col-contain {
overflow: hidden;
}
.col-one {
float: left;
width: 50%;
}
.logo {
vertical-align: middle;
}
.radio-spacer {
height: 20px;
}
.width-100 {
width: 100%;
}
</style>
</head>
<body>
<div class="sidebar branding-below">
<div class="output"></div>
<form>
<div class="block form-group">
<label for="translated-text"><b>Insert URL</b></label>
<input class="width-100" id="shared-brief"/>
</div>
<div class="block" id="button-bar">
<button class="blue" id="test-btn">Test</button>
</div>
</form>
</div>
<div class="sidebar bottom">
<img alt="Add-on logo" class="logo" src="https://www.gstatic.com/images/branding/product/1x/translate_48dp.png" width="27" height="27">
<span class="gray branding-text">Translate sample by Google</span>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(function() {
$('#test-btn').click(printTest);
google.script.run.withSuccessHandler(onSuccess)
.withFailureHandler(showError).dummyFunc();
});
function printTest() {
console.log('test');
}
function showError() {
console.log('error')
}
function onSuccess() {
console.log('success')
}
</script>
</body>
</html>
appsscript.json
{
"timeZone": "Europe/Paris",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"oauthScopes": ["https://www.googleapis.com/auth/script.external_request",
"https://www.googleapis.com/auth/script.container.ui",
"https://www.googleapis.com/auth/documents",
"https://www.googleapis.com/auth/drive"]
}
If I am in the editor and run dummyFunc, I can see in Log the ID of the Doc. But if I test my addon I keep getting 'error' from the client-side function showError.
What am I doing wrong?
I was able to make this working simply disabling the new V8 runtime. Really strange behavior and, above all, completely undocumented.