Creating a Google Workspace add on using Apps Script — variable isn't being properly referenced across functions

huangapple go评论76阅读模式
英文:

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

我在让不同的函数引用一个变量的值方面遇到了问题

  1. I have a startDate variable defined at the root set to "today"

  2. 我在根部定义了一个名为startDate的变量,其值设置为"today"

  3. In createDate it gets set to "tomorrow"

  4. 在createDate函数中,它被设置为"tomorrow"

  5. It then displays it correctly in a card header as "Default start tomorrow"

  6. 然后它在卡片标题中正确显示为"Default start tomorrow"

  7. When you click on the button, reviseStartDate is called

  8. 当您点击按钮时,将调用reviseStartDate函数

  9. A new card is created, and it's header incorrectly says "Revised start today" instead of "Revised start tomorrow"

  10. 创建了一个新的卡片,但其标题不正确地显示为"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中,这将正常工作。谢谢

Google add-ons docmentation

Google插件文档

Google Apps Script reference

Google Apps Script参考文档

英文:

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

  1. I have a startDate variable defined at the root set to "today"
  2. In createDate it gets set to "tomorrow"
  3. It then displays it correctly in a card header as "Default start tomorrow"
  4. When you click on the button, reviseStartDate is called
  5. 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

Google add-ons docmentation

Google Apps Script reference

答案1

得分: 0

Google Apps Script 处理这些变量的方式不同,它们在每次执行中都会被重新评估,调用脚本以创建卡片被视为单独的执行:

  1. 脚本运行以创建一个卡片,这个卡片现在显示在侧边栏中,但这并不意味着脚本仍在执行,GAS已经完成了所请求的操作,即创建卡片。
  2. 在已创建的卡片中使用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 handles these variables differently, they are evaluated in every execution and calling the script to create a card is considered a separate execution:

  1. 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.
  2. 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:

huangapple
  • 本文由 发表于 2023年5月22日 12:35:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定