This is not the same than Object property values lost on server side when object passed via google.script.run
I'm trying to set my Google Apps Script add-on test framework. Since Test as add-on can't be used to test installable triggers, I'm wondering if I could pull server side code from a bounded project to be use as a library to test my sidebars that call server-side functions.
If I run the below code using Run > Test as add-on... it shows my menu two times
- As a custom menu named "Sidebar", next to the Help menu
- As an add-on menu, on Add-ons > My Project
- When I click on Sidebar > Open, clicking on the sidebar buttons
getMail()
returnsundefined
- When I click on Add-on > My Project > Open, clicking on the sidebar buttons
getMail()
, as expected, returns the active user email address.
The same happens, server side object lost, when I replace the getMail()
function by another that use the Spreadsheet Service to return a cell value or even by one that return a primitive string.
What am I missing?
The bottom line is that I want to add a button on my sidebar that creates an installable trigger and get some values from the spreadsheet.
Spreadsheet to be used as library
Code.gs
function onOpen(e) {
var ui = SpreadsheetApp.getUi();
var menu = ui.createMenu('Sidebar');
menu
.addItem('Open', 'showSidebar')
.addToUi();
}
function showSidebar() {
var ui = HtmlService.createHtmlOutputFromFile('Sidebar')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('A Sidebar');
SpreadsheetApp.getUi().showSidebar(ui);
}
function getEmail() {
return Session.getActiveUser().getEmail();
}
Sidebar.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
function updateButton(email, button) {
console.log(email);
button.value = 'Clicked by ' + email;
}
</script>
</head>
<body>
<input type="button" value="Not Clicked" onclick="google.script.run
.withSuccessHandler(updateButton)
.withUserObject(this)
.getEmail()" />
<input type="button" value="Not Clicked" onclick="google.script.run
.withSuccessHandler(updateButton)
.withUserObject(this)
.getEmail()" />
</body>
</html>
Examples of other functions used to replace getMail()
function getCellValue(){
return SpreadsheetApp.getActiveCell().getValue();
}
function getGreeting(){
return 'Hello world';
}
Spreadsheet to be used as library client
- Add the Library to the project
Add the below code
function onOpen(e) { aLib.onOpen(e); }
function showSidebar(){ aLib.showSidebar(); }
function getEmail(){ aLib.getEmail(); }
Test as add-on...
- Click on Run > Test as add-on
- Add "Spreadsheet to be used as library client " as doc to be used to test as add-on
- Launch the doc