Gmail附加传递参数可通过卡片操作触发功能 [英] Gmail Add-on pass parameters to function triggered by card action
问题描述
我正在构建一个Gmail加载项,该加载项仅用一个表单按钮即可创建一张卡片.单击后,我希望按钮触发一个功能,该功能会将打开的电子邮件的内容发送到外部API.
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);
}
如何将 currentMessage
变量传递给 callAPI
函数?根据文档,我们可以从动作函数中获得的唯一参数似乎是 event
,它仅具有表单字段数据.如果没有传递其他参数的方法,该函数是否有办法直接在函数内部获取消息的上下文数据?
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??
谢谢!
推荐答案
您可以使用属性服务,可让您将字符串存储为键值对.想法是将 currentMessage
的值存储在函数 createCard
中的属性中,然后在 callAPI
中使用它.
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
.
- 首先,要存储变量,可以在声明
currentMessage
后将以下行添加到createCard
中:
- First, to store the variable, you can add the following lines to
createCard
after declaringcurrentMessage
:
var userProperties = PropertiesService.getUserProperties();
userProperties.setProperty("currentMessage", currentMessage);
- 第二,要检索此变量,请将以下行添加到
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.
- 您首先必须通过子字符串,并将数组的每个元素存储在不同的属性中.您必须在声明
currentMessage
后将其添加到createCard
中:
- 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
参考:
- 物业服务
- String.prototype.substring()
- String.prototype.concat()
我希望这会有所帮助.
这篇关于Gmail附加传递参数可通过卡片操作触发功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 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