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
 
    