简体   繁体   中英

How to pass a JS object to a server function using google.script.run in Google Apps Script?

I want to pass an object to a server function using Google Apps Script. But I'm having a problem doing it this way.

<?= config.bob ?>
<button type="button" onclick="google.script.run.sayHiObject(<?= config ?>)">
  Run Bob
</button>

When I click the button labeled Run Bob , I expect to see an alert prompt that says:

Hi, Bob!

But instead, it says:

Hi, undefined!

This page says:

Legal parameters are JavaScript primitives like a Number, Boolean, String, or null, as well as JavaScript objects and arrays that are composed of primitives, objects, and arrays. [Emphasis mine]

So, I assume it's possible to pass an object. However, my demo shown below only proves it's possible to pass a string that way. The object seems to have a problem.

What am I doing wrong?

Fig. 1. Demo. Run Alice and Run Charlie successfully pass strings as arguments. But Run Bob fails to pass an object as the argument.

在此处输入图片说明

Code.gs
 var TITLE = 'Say hi to:'; var HTML_FILENAME = 'index'; var ui = SpreadsheetApp.getUi(); function handleEdit(e) { var template = HtmlService.createTemplateFromFile(HTML_FILENAME); template.alice = 'Alice'; template.config = { bob: 'Bob', charlie: 'Charlie' }; var htmlOutput = template.evaluate(); ui.showModalDialog(htmlOutput, TITLE); } function sayHi(name) { var msg = 'Hi, ' + name + '!'; ui.alert(msg); } function sayHiString(name) { sayHi(name); } function sayHiObject(config) { sayHi(config.name); }
index.html
 <!DOCTYPE html> <html> <body> <?= alice ?> <button type="button" onclick="google.script.run.sayHiString(<?= alice ?>)"> Run Alice </button> <?= config.bob ?> <button type="button" onclick="google.script.run.sayHiObject(<?= config ?>)"> Run Bob </button> <?= config.charlie ?> <button type="button" onclick="google.script.run.sayHiString(<?= config.charlie ?>)"> Run Charlie </button> </body> </html>

How about this answer? Please think of this as just one of several possible answers.

When template.config = { bob: 'Bob', charlie: 'Charlie' , name: "sample"}; is used as <?= config ?> , <?= config ?> becomes [object Object] . By this, such issue occurs. On the other hand, <?= config.charlie ?> becomes 'Charlie' which is a string. By this, the script works. So please use the string type as the value.

In order to work your script, how about the following modification?

Modified script:

From:

<button type="button" onclick="google.script.run.sayHiObject(<?= config ?>)">

To:

<button type="button" onclick="google.script.run.sayHiObject(JSON.parse(<?= JSON.stringify(config) ?>))">

And also, in your script, { bob: 'Bob', charlie: 'Charlie' } has no name propery. So for example, also please modify as follows.

From:

template.config = { bob: 'Bob', charlie: 'Charlie' };

To:

template.config = { bob: 'Bob', charlie: 'Charlie', name: 'sample' };

Reference:

If I misunderstood your question and this was not the direction you want, I apologize.

Hi try to change config to config.bob

in <button type="button" onclick="google.script.run.sayHiObject(<?= config ?>)">

like this

in <button type="button" onclick="google.script.run.sayHiObject(<?= config.bob ?>)">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM