Home > Net >  Google Apps Scripts: How to keep track of selected dates with CardService.newDatePicker?
Google Apps Scripts: How to keep track of selected dates with CardService.newDatePicker?

Time:06-23

Use selects a date from CardService.newDatePicker:

const DatePickerFrom = CardService.newDatePicker()
    .setTitle("From Day")
    .setFieldName("range_start")
    .setValueInMsSinceEpoch(todayDate.getTime())
    .setOnChangeAction(
      CardService
        .newAction()
        .setFunctionName("handleRangeStartChange")
    );

I need to remember what date they select to perform other actions later...

const scriptProperties = PropertiesService.getScriptProperties();

scriptProperties.setProperties({
  "rangeStart": new Date(),
  "rangeEnd": new Date(),
  "dayStartHour": 9,
  "dayStartMinutes": 0,
  "dayEndHour": 17,
  "dayEndMinutes": 0,
  "busyBlocks": []
});

function handleRangeStartChange(e) {
  const rangeStart = formatDate(new Date(e.formInput.range_start.msSinceEpoch));
  scriptProperties.setProperty("rangeStart", rangeStart);
}

But later on, when I log the values from scriptProperties, the updated values are NOT there. It logs default same values from setProperties.

function logParams() {
  const params = scriptProperties.getProperties();
  Logger.log(params)
}

How is it possible to keep track of user selected values when building Google Add-Ons?

FYI, my Add-On is a Card Add-On for Calendar.

CodePudding user response:

PropertiesService Saves everything as strings I believe so perhaps this would work:

I tried this simple example and it works okay:

function setProps() {
  PropertiesService.getScriptProperties().setProperty("mydate",new Date().valueOf().toFixed(0));
  Logger.log(PropertiesService.getScriptProperties().getProperty("mydate"));
  Logger.log(new Date(Number(PropertiesService.getScriptProperties().getProperty("mydate"))))
}

Execution log
4:47:45 PM  Notice  Execution started
4:47:45 PM  Info    1655851665625
4:47:45 PM  Info    Tue Jun 21 16:47:45 GMT-xxxx 2022
4:47:46 PM  Notice  Execution completed

So with your function:

function handleRangeStartChange(e) {
  const rangeStart = new Date(e.formInput.range_start.msSinceEpoch.toFixed(0));//not sure about this
  scriptProperties.setProperty("rangeStart", rangeStart);
}

I think you will want to do the same thing with setting script prooperties

scriptProperties.setProperties({
  "rangeStart": new Date().valueOf().toFixed(0),
  "rangeEnd": new Date().valueOf().toFixed(0),
  "dayStartHour": 9,
  "dayStartMinutes": 0,
  "dayEndHour": 17,
  "dayEndMinutes": 0,
  "busyBlocks": []
});
  • Related