Update Google Calendar UI after changing visability setting via Workspace Add-On
Asked Answered
D

2

3

I have a very basic Google Workspace Add-on that uses the CalendarApp class to toggle the visabilty of a calendar’s events when a button is pressed, using the setSelected() method

The visabilty toggling works, but the change in only reflected in the UI when the page is refreshed. Toggling the checkbox manually in the UI reflects the change immediately without needing to refresh the page.

Is there a method to replicate this immediate update behaviour via my Workspace Add-On?

A mwe is below.

function onDefaultHomePageOpen() {

  // create button
  var action = CardService.newAction().setFunctionName('toggleCalVis')
  var button = CardService.newTextButton()
    .setText("TOGGLE CAL VIS")
    .setOnClickAction(action)
    .setTextButtonStyle(CardService.TextButtonStyle.FILLED)
  var buttonSet = CardService.newButtonSet().addButton(button)

  // create CardSection
  var section = CardService.newCardSection()
    .addWidget(buttonSet)

  // create card
  var card = CardService.newCardBuilder().addSection(section)

  // call CardBuilder.call() and return card
  return card.build()

}

function toggleCalVis() {
  // fetch calendar with UI name "foo"
  var calendarName = "foo"
  var calendarsByName = CalendarApp.getCalendarsByName(calendarName)
  var namedCalendar = calendarsByName[0]

  // Toggle calendar visabilty in the UI
  if (namedCalendar.isSelected()) {
    namedCalendar.setSelected(false)
  }
  else {
    namedCalendar.setSelected(true)
  }
}
Drumhead answered 23/1, 2021 at 1:17 Comment(0)
C
3

In short: Create a chrome extension

(2021-sep-2)Reason: The setSelected() method changes ONLY the data on server. To apply the effect of it, you need to refresh the page. But Google Workspace Extension "for security reason" does not allow GAS to do that. However in an Chrome Extension you can unselect the checkbox of visibility by plain JS. (the class name of the left list is encoded but stable for me.) I have some code for Chrome Extension to select the nodes although I didn't worked it out(see last part).

(2021-jul-25)Worse case: Default calendars won't be selected by getAllCalendars(). I just tried the same thing as you mentioned, and the outcome is worse. I wanted to hide all calendars, and I am still pretty sure the code is correct, since I can see the calendar names in the console.

const allCals = CalendarApp.getAllCalendars()
allCals.forEach(cal => {console.log(`unselected ${cal.setSelected(false).getName()}`)})

Yet, the principle calendar, reminder calendar, and task calendar are not in the console.

And google apps script dev should ask themselves: WHY DO PEOPLE USE Calendar.setSelected()? We don't want to hide the calendar on the next run.

In the official document, none of these two behaviour is mentioned.

TL;DR part (My reason for not using GAS)

GAS(google-apps-script) has less functionality. For what I see, google is trying to build their own eco-system, but everything achievable in GAS is also available via javascript. I can even use typescript and do whatever I want by creating an extension.

GAS is NOT easy to learn. The learning was also painful, I spent 4 hours to build the first sample card, and I can interact correctly with the opened event after 9 hours. The documentation is far from finished.

GAS is poorly supported. The native web-based code editor (https://script.google.com/) is not build for coding real apps, it loses the version control freedom in new interface. And does not support cross-file search. Instead of import, codes run from top to bottom in the list, which you need to find that by yourself. (pass along no extension, no prettier, I can tolerate these) In comparison with other online JS code editors, like codepen / code sandbox / etcetera it does so less function. Moreover, VSCode also has a online version now(github codespaces).

I hope my 13 hours in GAS are not totally wasted. As least whoever read this can just avoid suffering the same painful test.

Here's the code(typescript) for disable all the checks in Chrome. TRACKER_CAL_ID_ENCODED is the calendar ID of which I don't want to uncheck. Since it is not the major part of this question, it is not very carefully commented.

(line update: 2022-jan-31) Aware that the mutationsList.length >= 3 is not accurate, I cannot see how mutationsList.length works.

Extension:


getSelectCalendarNode()
  .then(unSelectCalendars)
function getSelectCalendarNode() {
  return new Promise((resolve) => {
    document.onreadystatechange = function () {
      if (document.readyState == "complete") {
        const leftSidebarNode = document.querySelector(
          "div.QQYuzf[jsname=QA0Szd]"
        )!;
        new MutationObserver((mutationsList, observer) => {
          for (const mutation of mutationsList) {
            if (mutation.target) {
              let _selectCalendarNode = document.querySelector("#dws12b.R16x0");
              // customized calendars will start loading on 3th+ step, hence 3, but when will they stop loading? I didn't work this out
              if (mutationsList.length >= 3) {
                // The current best workaround I saw is setTimeout after loading event... There's no event of loading complete.
                setTimeout(() => {
                  observer.disconnect();
                  resolve(_selectCalendarNode);
                }, 1000);
              }
            }
          }
        }).observe(leftSidebarNode, { childList: true, subtree: true });
      }
    };
  });
}

function unSelectCalendars(selectCalendarNode: unknown) {
  const selcar = selectCalendarNode as HTMLDivElement;
  const calwrappers = selcar.firstChild!.childNodes; // .XXcuqd
  for (const calrow of calwrappers) {
    const calLabel = calrow.firstChild!.firstChild as HTMLLabelElement;

    const calSelectWrap = calLabel.firstChild!;
    const calSelcted =
      (calSelectWrap.firstChild!.firstChild! as HTMLDivElement).getAttribute(
        "aria-checked"
      ) == "true"
        ? true
        : false;
    // const calNameSpan = calSelectWrap.nextSibling!
    //   .firstChild! as HTMLSpanElement;
    // const calName = calNameSpan.innerText;
    const encodedCalID = calLabel.getAttribute("data-id")!; // const decodedCalID = atob(encodedCalID);
    if ((encodedCalID === TRACKER_CAL_ID_ENCODED) !== calSelcted) {
      //XOR
      calLabel.click();
    }
  }
  console.log(selectCalendarNode);
  return;
}

Curren answered 25/7, 2021 at 2:8 Comment(0)
H
0

There is no way to make a webpage refresh with Google Apps Script

Possible workarounds:

  • From the sidebar, provide users a link that redirects them to the Calendar UI webpage (thus a new, refreshed version of it will be opened)
  • Install a Goole Chrome extension that refreshes the tab in specified intervals
Huddleston answered 25/1, 2021 at 8:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.