Skip to content
Advertisement

How to Implement LockService in Google App Script The Right Way?

Currently, I’m trying lockservice in Google App Script to handle concurrency on my webapp. However, when I implement it, it did not work.

Description of my webapp: enter image description here

Whenever a user click on the button, it will get the website address value in the google sheet and open a new window to go to the website. Then, it will update the value in my google sheet with the value of a new website address.

on this webapp, it will cycle through 3 websites Google -> Youtube -> Facebook

The problem:

When I tried testing it for concurrency with clicking the button fast, it will return the same website. For example, if I clicked it twice, it will open 2 new window of Google, but the value in the google sheet will be updated to Facebook, which skip the Youtube.

I tried implement lockservice but it did not work, maybe I implement it wrongly.

this is my HTML code:

JavaScript

My code.gs:

JavaScript

ps. I tried two different ways (on the comment part in code.gs) that I can find on Stackoverflow and youtube but still no success.

Please help. Thank you.

Advertisement

Answer

I thought that in your script, when the button is clicked, google.script.run.withSuccessHandler(openWebsite).getWebsite() is run and also google.script.run.setWebsite() is run in the function openWebsite. In this case, the LockService is used for each function of getWebsite() and setWebsite(). I thought that this might be the reason of your issue. So, in your script, how about the following modification? In this modification, one LockService is used for the functions of getWebsite() and setWebsite().

HTML&Javascript side:

Please modify openWebsite(web) in your Javascript side as follows.

From:

JavaScript

To:

JavaScript
  • In this modification, google.script.run.setWebsite(); is removed.

Google Apps Script side:

Please modify getWebsite() in your Google Apps Script side as follows.

From:

JavaScript

To:

JavaScript
  • In this modification, google.script.run.setWebsite(); is removed.

Note:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement