英文:
Creating a Google Workspace add on using Apps Script — variable isn't being properly referenced across functions
问题
I'm writing an Google Workspace add-on for Google Calendar, using Google Apps Script.
我正在编写一个用于Google Calendar的Google Workspace插件,使用Google Apps Script。
I'm having trouble getting different functions to reference the value of a variable
我在让不同的函数引用一个变量的值方面遇到了问题
-
I have a startDate variable defined at the root set to "today"
-
我在根部定义了一个名为startDate的变量,其值设置为"today"
-
In createDate it gets set to "tomorrow"
-
在createDate函数中,它被设置为"tomorrow"
-
It then displays it correctly in a card header as "Default start tomorrow"
-
然后它在卡片标题中正确显示为"Default start tomorrow"
-
When you click on the button, reviseStartDate is called
-
当您点击按钮时,将调用reviseStartDate函数
-
A new card is created, and it's header incorrectly says "Revised start today" instead of "Revised start tomorrow"
-
创建了一个新的卡片,但其标题不正确地显示为"Revised start today"而不是"Revised start tomorrow"
Here is my code, you can run it as-is:
这是我的代码,您可以直接运行它:
var startdate = "today"; // Define startdate as a global variable with an initial value
var startdate = "today"; // 将startdate定义为具有初始值的全局变量
function onOpen() {
var card = createCard();
return card;
}
function createCard() {
startdate = "tomorrow"; // Update the value of startdate
var button = CardService.newTextButton()
.setText('Revise start date')
.setOnClickAction(CardService.newAction()
.setFunctionName('reviseStartDate'));
var section = CardService.newCardSection()
.addWidget(button);
var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle('Default start:' + startdate))
.addSection(section)
return card.build();
}
function reviseStartDate() {
var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle("Revised start:" +startdate));
return card.build();
}
And here is part of my manifest
以下是我的清单的一部分
"calendar": {
"homepageTrigger": {
"runFunction": "onOpen"
},
"eventOpenTrigger": {
"runFunction": "showFreeTime"
}
}
"calendar": {
"homepageTrigger": {
"runFunction": "onOpen"
},
"eventOpenTrigger": {
"runFunction": "showFreeTime"
}
}
How can I allow start date to be read and written by multiple functions, in "regular" javascript this would work fine. Thanks
如何允许多个函数读取和写入开始日期,在"常规"JavaScript中,这将正常工作。谢谢
英文:
I'm writing an Google Workspace add-on for Google Calendar, using Google Apps Script.
I'm having trouble getting different functions to reference the value of a variable
- I have a startDate variable defined at the root set to "today"
- In createDate it gets set to "tomorrow"
- It then displays it correctly in a card header as "Default start tomorrow"
- When you click on the button, reviseStartDate is called
- A new card is created, and it's header incorrectly says "Revised start today" instead of "Revised start tomorrow"
Here is my code, you can run it as-is:
var startdate = "today"; // Define startdate as a global variable with an initial value
function onOpen() {
var card = createCard();
return card;
}
function createCard() {
startdate = "tomororw"; // Update the value of startdate
var button = CardService.newTextButton()
.setText('Revise start date')
.setOnClickAction(CardService.newAction()
.setFunctionName('reviseStartDate'));
var section = CardService.newCardSection()
.addWidget(button);
var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle('Default start:' + startdate))
.addSection(section)
return card.build();
}
function reviseStartDate() {
var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle("Revised start:" +startdate));
return card.build();
}
And here is part of my manifest
"calendar": {
"homepageTrigger": {
"runFunction": "onOpen"
},
"eventOpenTrigger": {
"runFunction": "showFreeTime"
}
How can I allow start date to be read and written by multiple functions, in "regular" javascript this would work fine. Thanks
答案1
得分: 0
Google Apps Script 处理这些变量的方式不同,它们在每次执行中都会被重新评估,调用脚本以创建卡片被视为单独的执行:
- 脚本运行以创建一个卡片,这个卡片现在显示在侧边栏中,但这并不意味着脚本仍在执行,GAS已经完成了所请求的操作,即创建卡片。
- 在已创建的卡片中使用UI运行
reviseStartDate()
函数,但这被视为新的和独立的执行(如执行日志中所示),这意味着变量会被重新评估。
我找到了这篇帖子,解释了类似的行为,并找到了一种涉及使用CacheService
的解决方法来存储您的变量,这种修改将产生所需的输出:
var cache = CacheService.getUserCache();
function onHomepage() {
var card = createCard();
return card;
}
function createCard() {
cache.put("startdate", "tomorrow");
var startdate = cache.get("startdate");
var button = CardService.newTextButton()
.setText('Revise start date')
.setOnClickAction(CardService.newAction()
.setFunctionName('reviseStartDate'));
var section = CardService.newCardSection()
.addWidget(button);
var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle('Default start: ' + startdate))
.addSection(section);
return card.build();
}
function reviseStartDate() {
var startdate = cache.get("startdate");
var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle("Revised start: " + startdate));
return card.build();
}
另一种替代方法是使用用户属性,其工作方式类似。
一些文档:
- Google Apps Script - Cache Service
英文:
Google Apps Script handles these variables differently, they are evaluated in every execution and calling the script to create a card is considered a separate execution:
- Script runs to create a card, this card is now displayed in the side bar but that doesn't mean that the script is still executing, GAS already completed the requested operation which was creating a card.
- Using the UI in the already created card runs the
reviseStartDate()
function but that is considered a new and separate execution (as shown in the execution log) meaning that the variables are re-evaluated.
I found this post that explained a similar behavior and found a workaround that involved using the CacheService
to store your variables, this modification would yield the desired output:
var cache = CacheService.getUserCache();
function onHomepage() {
var card = createCard();
return card;
}
function createCard() {
cache.put("startdate","tomorrow");
var startdate = cache.get("startdate");
var button = CardService.newTextButton()
.setText('Revise start date')
.setOnClickAction(CardService.newAction()
.setFunctionName('reviseStartDate'));
var section = CardService.newCardSection()
.addWidget(button);
var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle('Default start: ' + startdate))
.addSection(section)
return card.build();
}
function reviseStartDate() {
var startdate = cache.get("startdate");
var card = CardService.newCardBuilder()
.setHeader(CardService.newCardHeader().setTitle("Revised start: " + startdate));
return card.build();
}
Another alternative would be to use User Properties that work in a similar fashion.
Some Docs:
-
Google Apps Script - Cache Service
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论