Bad writing in GoogleSheets - google-apps-script

I've one sheet that works as database which gets the data from too many different sheets. It works in that logic, I've multiple sheets that everyone is filling the data in and then click on a button to trigger a script that takes the data and paste it in the database sheet, and sends an email with the data to the relevant stakeholder.
The issue I'm facing is that, if two sheets triggered the script at the same time one sheet will overwrite the other one because both were triggered at the same time and both were targeting the same row in the database.
I've tried the lock methods as well but it will not work because those are multiple sheets and multiple scripts, and tried to assign a ticket number property but it will not work as well because every script is operating on different sheet. thought of library but it will not be as fast as the normal script.

Related

Why do two users sometimes see different values from importrange?

I have a very strange problem, the description can be a bit chaotic.
I have two spreadsheets A and B. One sheet inside A is importing data with importrange from the B. The problem is that sometimes two user see different values in that sheet.
Details:
I have two scripts inside A:
one that copies the values from the imported range to another sheet
one that calls that WebApp that changes data in B.
When two users have the A open and one of them makes changes to the B via script and then uses the other script which copies the values, everything is fine. But if one user closes the A, the other makes changes in the B with the script and when the first user reopens the spreadsheet, he will see new values, but if he uses the values copying script, the range containing the values from importrange will show old values. Oddly enough, the script that copies values from that range will copy the new correct values even if we don't see them.
The problem goes away when I manually make some changes to spreadsheet B. I don't know, it's like spreadsheet B doesn't refresh when I make changes via script or something
Do you have any ideas what is the reason for this and maybe how to solve it. I think that I can get round this problem but I would not.
Two Google Sheets users might see diffent values on the same spreasheet because formula calculation (including those that make API calls like IMPORTRANGE) are triggered by user actions like opening or edited the spreadsheet.
Some function also might recalculated by changes made by scripts so it's actually possible that an script could "see" different values than those that are seeing by the users.
If you need that the users see the same values, should consider to periodically "freeze" them i.e. make a copy of the spreadsheet then replace the formulas by their results.
Another option is to share the screen rather than relaing on the "real time" collaboration feature of Google Sheets.

Google Sheet Scripts and "IMPORTDATA" stoped working after years of working

I have 6 copies of the same spreadsheet that is used for a running race management. Each sheet may be used for different races that occur on the same weekend but in different locations.
These have worked fine for years. They have various scripts that do things like clear a section of cells, move data from sheet to sheet, etc, pretty basic stuff. Other sheets access these sheets using IMPORTDATA, but only a single cell.
Several of the sheets had scripts stop working, and any sheet trying to access a cell from that sheet using IMPORTDATA also does not work giving a #ref error.
Making a copy of a non-functioning sheet will work for a day or two before scripts and Importdata stop working again.
I am wondering if I am reaching some Google limit, the only errors i receive in Summary of Failures email are nothing unusual , "service timed out" of course because script is not working.

How to allow users to run a macro which edits a range or sheet they don't have 'edit' permissions to?

I have made a macro that takes a set of input data, transfers it to a master data table, graphs trend lines of the data, and then clears the data entry sheet. All sheets are protected to avoid employees from changing or deleting data that has already been entered and only the cells which require data entry are available for editing by the employees who I've shared the sheet with. The macro works well if I try to run it but if another employee attempt to run the macro they get an error stating they don't have permission to edit certain ranges.
When I wrote the Excel macro for the same task I was able to unlock the sheets up front, transfer the required data, and then re-lock the sheets but can't figure out how to replicate this type of behavior in Google Apps Script.
The Protection class on the Google Developers page did not prove to be helpful, since I kept getting an error saying the function protect could not be located in the Object spreadsheet. I also tried the addEditor function without much luck. This is my first Google Apps Script and I've been trying to learn as I go but this project is proving difficult.
Thank you to #Tedinoz for helping me realize you can rename the onEdit function so long as you designate 'e' as a function input. I was able to use an Installed Trigger on the sheet set to run RenamedonEdit(e) whenever the sheet is edited.

Macro to delete the old data

I'm really new to this and would love some help where I'm currently stuck. I've created a macro to clear a spreadsheet. I've tried multiple triggers of onedit and onchange and haven't had luck with my end goal. I'm sending data (about 10 columns and 100 rows into a google sheets via zapier. Basically, I'm refreshing the data by replacing the data. So, before the data comes in, I wanted the macro to delete the old data. However, the macro actually deletes the new data coming in once it posts. Any scripts or workarounds that could help?
From what I know about Zapier it works on timed intervals. You can have a script run on time intervals just before the time Zapier inputs information and delete the old info.
Another approach would probably be to have a intermediary sheet.(Lets call this sheet, "sheet1". The final sheet can be called "sheet2". When Zapier writes data to sheet1 it can trigger the onChanged event. Take the information in sheet2 delete everything, then post the new data to sheet 2.
Finally, Zapier supports webhooks. You can publish your script as a web app and have it do a get or post to the app which runs the delete function. To give it time you can use the delay that Zapier provides.
What you are describing sounds like the perfect candidate for the IMPORTDATA worksheet function. The function is entered in a cell, as are all worksheet functions, and it takes a URL which points to a CSV or TSV file. Once entered the data referred to by the URL is expanded out into the appropriate number of rows and columns. Seeing as the function resides in a single cell you would only need to update that cell when you wanted to change the data. This means that you would no longer need to use a .gs file to remove old data and could instead complete everything from within Zapier. I have answered a question similar to this here, I describe how this is done within Zapier. The only prerequisite is that the data you are using is in either CSV or TSV format.

Running Google spreadsheet script from a different spreadsheet

I have several spreadsheets that collect data from multiple/different forms and I can run a script bound to that spreadsheet based on form submit.
However, what I would like to do is to have the script of the form spreadsheet access and run the script of a main spreadsheet.
The main spreadsheet collects and processes data from several different forms.
It seems that using a trigger in the main spreadsheet based onEdit only applies when a user is actually editing, not when another spreadsheet writes data to it.
Thanks for your time.
Take a look at this answer. It seems
OnEdit trigger is intended to work when an actual user edits a spreadsheet
If each of these other sheets is just adding a row to the main sheet, you can create a way that the main sheet keeps track of how many rows it knows are there. Then you set up a cron that runs every X minutes, checks if there are new rows, and does what it needs to on those.