google-apps-scriptgoogle-appsgsuite-addons

How do I save data within my Google Suite add-on


I am making a G-Suite Drive add-on. I have a button that makes a G-sheet. I then can edit that G-sheet from the add-on in a few ways. However, upon reloading the add-on on some later date, I would like it to remember the id of that spreadsheet so it can keep editing it. How do I save the spreadsheet id to my Google Suite add-on?

I don't want to make the G-sheet a certain name then just search the name because users will be able to change the name.

EDIT: As seen in comment, the best way to do this is with propertiesService and the fact that it is a G-Suite add-on is irrelevant.


Solution

  • You can use UserProperties

    This is allow you to store values, like e.g. a spreadsheetId individually for each user who uses your script.

    Sample:

    var newId = SpreadsheetApp.getActive().copy("new Sheet").getId();
    var userProperties = PropertiesService.getUserProperties();
    userProperties.setProperty('sheetId', newId);