Running Google spreadsheet script from a different spreadsheet - google-apps-script

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.

Related

Is there a way to set up Google Apps Script trigger to be based on the edit of one of multiple spreadsheets?

I want to send an email when one of three spreadsheets have been edited. I have set up a trigger in the UI that runs a function on edit for one spreadsheet, but how do I make it so it runs on edit for one of three spreadsheets?
One solution is I put the function in scripts for each file and set up separate triggers, but is there a way to do this without having three scripts?
When I say spreadsheet, I mean a google sheets file
If the information required isn't time sensitive, Create a time based trigger on a standalone script, which checks each of the three spreadsheet files every hour or so for modifications and sends a email, if modifications are inferred. It is possible to use PropertiesService
to store last modified date time and compare it to the current modified date time.
get last row of sheet and compare it to last row currently, if data is added in that format.
There is no way to make that a single on edit trigger, simple or installable, be triggered by the edits made on several spreadsheets.
One option is to use code to create multiple triggers to run the same function, one trigger for each spreadsheet.
Reference
https://developers.google.com/apps-script/guides/triggers
https://developers.google.com/apps-script/guides/triggers/installable

Bad writing in GoogleSheets

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.

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.

Google script: unprotect a sheet through function when user has no right to do so

I have a spreadsheet with different sheets in Google sheet, 3 users can edit each one a sheet (protections are set, each user can edit only one sheet). They all can execute a google script function that writes what they edited in a summary sheet. I don't want anyone to be abble to edit the summary sheet, so I set myself as the only available editor.
So my problem is to authorize the 3 users, only through the google script function, to write in the summary sheet. I tried to use the following function :
var unprotected = summarySheet.getRange('G3:G10');
protection.setUnprotectedRanges([unprotected]);
but since the users are not allowed to edit the summary sheet, and since the function is run with the active user, so they can't give themselves the right to unprotect a range in the summary sheet... Do you know how to workaround this problem?
Thanks a lot!
I see two script-based choices, one easy and one quite hard, and one sheet-based choice, that is easiest:
Easy:
You run the "summarize" script instead of them or, you set the summarize script run on a trigger out of your account. Then you actually leave protections alone. You could set the summarize script to run on open with error catching if the user doesn't have the necessary authority to unprotect the summary sheet and/or write to the summary sheet.
Hard:
When they run the "summarize" script it calls a published standalone script that has been given the authorization to make the necessary protection changes. I'll be honest, I wouldn't be able to code this but have seen/heard of similar implementations.
Easiest:
Finally, I want to make sure you've considered having the summary sheet itself contain the necessary formulas, parsing, etc. to summarize data from the other sheets without any need of scripts for this aspect of the sheet. The sheet could call custom functions as needed if the parsing or other summarization functionality is beyond built-in functions' capabilities. The sheet could stay fully protected and update itself in real time as users enter data (no need for users to trigger the summary creation, unless spreadsheet settings have auto-recalculate turned off).
Edited to add: put in A1 of Summary sheet something like:
=summarize()
And have that custom function return a 2-dimensional array of the summarized data.

Change permissions on the Spreadsheet

I have a script (Script by spreadsheet) who writes a series of data in a spreadsheet, but some parts of the spreadsheet are locked.
The users that are running this script from the spreadsheet are blocked in certain areas, it is possible that Script release these areas and then lock them again or run as adm of the Spreadsheet?
Thank you.
The user would need to have permission to edit sharing on the Spreadsheet, which would not be the case if it was locked.
Is the script an onEdit trigger? If so, you can add a trigger from the script editor that will run as yourself. Just make sure to rename onEdit() to something else so it doesn't try to run as the regular user as well.
If this isn't onEdit, I would suggest making the function they call add a specific value to a specific cell. Then you can have a timed trigger every minute running as yourself that watches this cell and does its work when the cell contains the specific word. Just make sure to clear the cell quickly so it doesn't run twice.