问题
I'm building a Gmail add-on that creates a card with just one form button. Once clicked, I want the button to trigger a function that will send the open email's content to an external API.
So far, I have something like this:
function createCard(event) {
var currentMessage = getCurrentMessage(event).getBody();
var section = CardService.newCardSection();
var submitForm = CardService.newAction()
.setFunctionName('callAPI');
var submitButton = CardService.newTextButton()
.setText('Submit')
.setOnClickAction(submitForm);
section.addWidget(CardService.newButtonSet()
.addButton(submitButton));
var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader()
.setTitle('Click this button'))
.addSection(section)
.build();
return [card];
}
function callAPI(event) {
var payload = { "msg": msg }; // msg is the parameter I need to get from the function call
var options = {
"method" : "POST",
"contentType": "application/json",
"payload" : JSON.stringify(payload),
"followRedirects" : true,
"muteHttpExceptions": true
};
return UrlFetchApp.fetch('https://www.someAPI.com/api/endpoint', options);
}
How can I pass the currentMessage
variable into the callAPI
function? According to the documentation, the only parameter that we can get from an action function seems to be event
that only has the form fields data. If there isn't a way to pass other parameters, is there a way for that function to get the context data of the message directly inside the function??
Thanks!
回答1:
You can use Properties Service, which allows you to store strings as key-value pairs. The idea would be to store the value of currentMessage
in a property in the function createCard
, and then use it in callAPI
.
- First, to store the variable, you can add the following lines to
createCard
after declaringcurrentMessage
:
var userProperties = PropertiesService.getUserProperties();
userProperties.setProperty("currentMessage", currentMessage);
- Second, to retrieve this variable, add the following lines to
callAPI
:
var userProperties = PropertiesService.getUserProperties();
var msg = userProperties.getProperty("currentMessage");
Update:
In order to avoid the limit of characters for a single property, you could also split your message into as many properties as were necessary.
- You would first have to split the message into an array via substring, and store each element of the array in a different property. You would have to add this to
createCard
, after declaringcurrentMessage
:
var messageArray = [];
var i = 0;
var k = 5000; // Character limit for one property (change accordingly)
while (i < currentMessage.length) {
var part = currentMessage.substring(i, i += k);
messageArray.push(part); // Splitting string into an array of strings
}
var userProperties = PropertiesService.getUserProperties();
userProperties.deleteAllProperties(); // Delete old properties
for (var j = 0; j < messageArray.length; j++) { // Set a different property for each element in the array
userProperties.setProperty('messagePart' + j, messageArray[j]);
}
- Then, to retrieve the message body, you could first retrieve all the properties corresponding to the message, and then join all the values in a single string with concat. Add the following lines to
callAPI
:
var userProperties = PropertiesService.getUserProperties();
var keys = userProperties.getKeys(); // Get all property keys
var j = 0;
var currentMessage = "";
do {
var part = userProperties.getProperty('messagePart' + j);
currentMessage = currentMessage.concat(part); // Concat each property value to a single string.
j++;
} while (keys.indexOf('messagePart' + j) !== -1); // Check if property key exists
Reference:
- Properties Service
- String.prototype.substring()
- String.prototype.concat()
I hope this is of any help.
来源:https://stackoverflow.com/questions/60094336/gmail-add-on-pass-parameters-to-function-triggered-by-card-action