1

This issue is causing me quite a bit of worry... any help would be much appreciated.

I have have three spreadsheets, A, B, and C (I own all of them). I then create a script that lets one user transfer money from A to C, while another user transfers money from B to C. Both of those users always send the money to the exact same cell in C (let's call that cell "the bank account"). And, the money transfers tend to happen either simultaneously or almost simultaneously, so there is a high risk of collision-errors. And, just in case it matters, the script always runs "as me" (the admin), no matter which user ordered the execution of the script.

So here is the heart of my question: how can I temporarily lock the "bank account" cell in C while someone is transferring money from A to C, and then, once the transfer from A to C is complete, the transfer from B to C will automatically begin? And how can I do this without locking C entirely (a lot of people modify C all the time, and it would disrupt their productivity quite a bit for me to lock the entire spreadsheet, even if just for a few seconds).

Or maybe it would be better to create some sort of buffering system, instead of temporarily locking the cell?

Here is an example of what exactly I meant by "collision-error":



I will use the term "the bank account" to refer to the cell that is receiving the money.

The starting value of the bank account is 100 dollars.

Alice activates a script to add 800 dollars to the bank account and Bob activates a script to add 100 dollars to the bank account.

Alice's script reads the the bank account's current value as 100.

Bob's script reads the bank account's current value as 100.

Alice's script figures that the sum should be 900, and sets the new value of the bank account to be 900.

Bob's script figures that the sum should be 200, and sets the new value of the bank account to be 200.

The final output should have been 1000 dollars, but became 200 instead. So 800 dollars were lost.



Again, any help would be much appreciated.

  • You can try using [Lock Service](https://developers.google.com/apps-script/reference/lock) Also, you might want to use [SpreadsheetApp.flush()](https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app?hl=en#flush) in your code at the end. – Alan Wells May 10 '21 at 20:22

2 Answers2

0

Aside from using the Lock Service, you may also want to check the answer from a similar post about Preventing simultaneous users in Google Apps Script by using Properties Service, where you can implement a function that would prevent potential users from editing the file while it is currently edited by another user.

As per your question, yes, it is possible that data will be overridden or will cause collisions in running your script given there could be multiple users accessing your spreadsheet at the same time.

SputnikDrunk2
  • 3,398
  • 1
  • 5
  • 17
0

AFAIK what you are describing is know as race condition. Google Sheets and Google Apps Script haven't default measures to prevent this so you should define how this should be handled.

One resource Lock Service, another the Properties Service. Both have three scopes

  • Script
  • User
  • Document

In Google Sheets you also might make use of protections and in Google Drive files in general you might use the sharing settings.

Resources

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • Thank you very much for your reply. I have improved the phrasing of my question. Could you perhaps take a look at it, and help me with sussing out the best way to implement this? – Ronnie McDurgen May 13 '21 at 16:18