1

I'm using Google App Script. I want to reassign my Global variable named myname. I had reassigned the value of myname to Testing Name. It assigns the value to myname on click of Set Name Button as I console in the handleSetName function. But when I got the value of myname on the handleGetName function it's getting undefined. I had pasted the code below and attached the logger image so that you can see output.

How can I reassign global variables?

Can anyone guide me on this?

Code.gs

var myname = "";

function loadMTS(event) {
    const button1 = CardService.newTextButton().setText("Set Name")
        .setTextButtonStyle(CardService.TextButtonStyle.FILLED)
        .setOnClickAction(CardService.newAction().setFunctionName("handleSetName"));

    const button2 = CardService.newTextButton().setText("Get Name")
        .setTextButtonStyle(CardService.TextButtonStyle.FILLED)
        .setOnClickAction(CardService.newAction().setFunctionName("handleGetName"));

    return card.addSection(CardService.newCardSection().addWidget(button1).addWidget(button2)).build();
}

function handleSetName(e) {
    myname = "Testing Name";
    console.log("Name after change in handleSetName Fucntion :", myname);
}

function handleGetName(e) {
    console.log("Name : ", myname);
}

Ouput Image

Shiv Yadav
  • 467
  • 2
  • 11
  • Does this answer your question? [Creating a Google Workspace add on using Apps Script — variable isn't being properly referenced across functions](https://stackoverflow.com/questions/76303061/creating-a-google-workspace-add-on-using-apps-script-variable-isnt-being-prop) – Bryan Monterrosa Jul 13 '23 at 12:43
  • That will work fine. But maybe it saved the variable for all time in-app script. – Shiv Yadav Jul 13 '23 at 13:16

0 Answers0