问题
I have a Spreadsheet file with a bounded script which integrates a Library. This library provides 2 functions:
- Show an html modal with a button
- Provides the function callback when the button on the html is clicked
LIBRARY content:
test.html
<!DOCTYPE html>
<html>
<head>
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">
</head>
<body>
<a class="waves-effect waves-light btn" onclick="runButtonFunction()">My Button</a>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
<script>
function onSuccess() {
console.log("onSuccess");
}
function onFailure(error) {
console.error("onFailure");
console.error(error);
}
function runButtonFunction() {
console.log("runButtonFunction");
google.script.run
.withFailureHandler(onFailure)
.withSuccessHandler(onSuccess)
//.callbackTestInBounded();
.callbackTestInLibrary();
}
</script>
</body>
</html>
success.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<p>SUCCESS!</p>
</body>
</html>
script.gs
function showModal() {
console.info("execute showModal");
var html = HtmlService.createHtmlOutputFromFile('test.html');
SpreadsheetApp.getUi().showModalDialog(html, 'Test')
}
function callbackTestInLibrary() {
console.info("callbackTest");
htmlCallback();
}
function htmlCallback() {
console.info("execute htmlCallback");
var html = HtmlService.createHtmlOutputFromFile('success.html');
SpreadsheetApp.getUi().showModalDialog(html, 'Test')
}
And BOUNDED content
function onOpen() {
SpreadsheetApp.getUi().createMenu("TEST").addItem("show modal", "openModal").addToUi();
}
function openModal() {
console.info("execute openModal");
TestLibrary.showModal();
}
function callbackTestInBounded() {
console.info("execute callbackTest");
TestLibrary.htmlCallback();
}
function callbackTestInLibrary() {
// Empty, only for make it works
}
While the open modal is working fine, the html box is shown w/o problems, I'm having some issues with the callback when the button is pressed.
My hypothesis of why is now not working is related to the change to V8 runtime, as in the past I always created html modal functions which trigger functions that are declared inside bounded script, now it does not seems to be the same.
TEST #1
If I declare the actual callback function callbackTestInBounded
inside the bounded script, and in the HTML I use google.script.run.callbackTestInBounded();
, I get this behaviour:
"Uncaught error" in Chrome console,
and if I see XHR response I get this:
)]}'
[["op.exec",null,[1,[]
,"Script function not found: callbackTestInBounded","ScriptError"]
]
,["di",377,null,null,null,null,[]
,[]
,null,null,[]
,[]
,[]
]
]
So I thought... maybe the new runtime calls the function as an implementation from the library the shown the modal itself, so I switched to second scenario.
TEST #2
I switched the html to call google.script.run.callbackTestInLibrary();
, and clicking the button I get this:
userCodeAppPanel:15 Uncaught TypeError: google.script.run.withFailureHandler(...).withSuccessHandler(...).callbackTestInLibrary is not a function
at runButtonFunction (userCodeAppPanel:15)
at HTMLAnchorElement.onclick (userCodeAppPanel:1)
Apparently.. the functions is indeed retrieved from the bounded script and not from the library, so why the first test did not work? The actual mixed configuration I found to be working is:
HTML
google.script.run.callbackTestInLibrary();
BOUNDED
function callbackTestInLibrary() {
// Empty, only for make it works
}
LIBRARY (same name as bounded)
// The actual implementation of the function
function callbackTestInLibrary() {
console.info("callbackTest");
htmlCallback();
}
With all of this the second HTML is shown:
Is this a new kind of configuration need for V8 runtime when there is a scenario where both html and script-callback are inherited from a library? Is this to be expected?
来源:https://stackoverflow.com/questions/61173465/apps-script-modal-show-and-google-script-run-from-bounded-to-library