Script to create new sheets that are protected (Google Sheets) - google-apps-script

I already have a script for a button that save and create a new sheet of the saved version.
The goal is to modify a cashier sheet and to create a copy with the date and the name of the cashier. The only part missing for this script is that everytime a new version of the sheet is created it would be protected.
For example, on the picture, the first sheet is called Auberge and it's the one that we modify. Now, I would like that all the new sheets that we create when we press on the button Save, like the one called Alex 4 août, be protected automatically. Then, only the manager could modify the final versions of the cashiers sheets. The employees would only be able to modify the sheet called Auberge and then save it.

Related

Select Existing SpreadSheet in Google Appscript

I have an existing spreadsheet(named as 'abc')that i want to activate. I have written codes to open the spreadsheet on the server, but i do not know how to work on this opened spreadsheet such as editing the content. Please advice me how to work on this opened spreadsheet.
I do not need the physical spreadsheet to be opened for viewing. I only need to use codes to further edit the content inside after i opened my existing spreadsheet.
Below are the code:
var student_sheet=SpreadsheetApp.open(DriveApp.getFilesByName('abc').next())
//I want to edit this 'abc' spreadsheet.
You have to set a variable to it and then just update like you would an active spreadsheet.
function doSomething(){
var yourSpreadsheet = SpreadsheetApp.openById("???????")// enter your spreadsheet id
yourSpreadsheet.getRange("A1").setValue("boom"); // sets value in the spreadsheet
yourSpreadsheet.getRange("B2").clearContent();//deletes values.
}

Google Sheets Script To Check Changes From One Sheet File To Update Another Sheet URL

Apologies if something like this has already been asked, I have little to no experience with this.
Is there script that can check a Google Sheets workbook for edits to then affect a different Sheets workbook. I have found something along these lines but it always references the same workbook and moves the entire row.
I have a spreadsheet that contains multiple lists of users (Sample Move) and another staff member has a sheet containing activity status and other details (Sample Edit). When a user is placed on the Sample Edit spreadsheet, they would then need to be moved from their respective Active list and moved to the corresponding Inactive list. Users would not need to be automatically added back to the active list once they return, that would be a manual process.
Link to example Sample Move: https://docs.google.com/spreadsheets/d/1q3yRMa5sum8dHLyw_2ppLaWBUEoj_Gl6F_btWIiqsfw/edit?usp=sharing
Link to example Sample Edit: https://docs.google.com/spreadsheets/d/1IZzoJQkFsavgnjkwz1GkuwDj-m6kG-CYQfLKx2EmPSI/edit?usp=sharing

Switch from one spreadsheet to another in the same tab in Google Sheets with a script

I've been trying to build a script which I can use to switch from a given sheet in one spreadsheet, to a given sheet in another spreadsheet, in the same browser tab, where both spreadsheets are on the same google drive and in the same folder.
The purpose for doing this is I have more data than I can fit on a single google sheets spreadsheet (More than 200 individual sheets are needed) and I'd like to be able to navigate between them as seamlessly as possible.
I could have sworn I had a simple script like this working at some point, but I'm not sure. The script does appear to run, but it there is no result, it doesn't actually open the other sheet.
I've tried using both .openByID and .openByUrl, and both appear to give me the same result.
I set up two new spreadsheets, A and B, each with their own script, A pointing to B, and B pointing to A. The destination sheet within the spreadsheet has been named MainA and MainB.
//This is your "Go Home" Button
//URL of A: https://docs.google.com/spreadsheets /d/1LzsxZ3cCcOltELM_0T4VwipqYv5BbBDj9ugAZcciNHQ/edit#gid=0
//URL of B: https://docs.google.com/spreadsheets/d/1wrjr9VSsHb63RKz87JQbe20mCo1CteKHtsAiUGrb6O0/edit#gid=0
//ID of A: 1LzsxZ3cCcOltELM_0T4VwipqYv5BbBDj9ugAZcciNHQ
//ID of B: 1wrjr9VSsHb63RKz87JQbe20mCo1CteKHtsAiUGrb6O0
function goto_a() {
var ss = SpreadsheetApp.openById("1LzsxZ3cCcOltELM_0T4VwipqYv5BbBDj9ugAZcciNHQ")
ss.setActiveSheet(ss.getSheetByName("MainA"));
}
Obv the script for A is similar to this one, but with differently named function, destination sheet, and spreadsheet ID
In both cases, I'm using a button (drawing image) with the relevant script attached. If need be, I can share these spreadsheets so that the script can be tested.
Any advice would be great, thanks in advance.
As written in the official documentation ,
//Note that the spreadsheet is NOT physically opened on the client side.
// It is opened on the server only (for modification by the script).
You can alternatively provide a link using Browser.msgBox() for the user to click.
Related Question

Connect two Google Sheets with script

I want to create a sheet by SpreadsheetApp.create(); and to fill a QUERY in this new generated sheet via cell.setFormula and IMPORTRANGE to get data of another Sheet.
It doesn't work because the sheets are not connected. If I do so manually the QUERY works.
Is there a way to connect two sheets via script?
You could create a variable with the locations of the sheets by the ID's. Then, access the sheet remotely via the script. Save the cell variable and paste this over into the first sheet.
Psuedo-Code Example
Sheet1 = SpreadsheetApp.getSheetByID('IDofSheetOne');
SpreadsheetApp.create();
Sheet2 = SpreadsheetApp.getSheetByID('IDofSheetTwo'); // the one just created, you'd probably have to use some kind of get to get this ID.
Now from here, you can just say, Sheet1.doWhatever() and Sheet2.doWhatever().
Thats how you can access more than one sheet at a time.

How to make new sheet in google spreadsheet selected?

My script adds new sheet on a spreadsheet after some form submission. This sheet is used as a leaderboard. But to look on it I have to click on the new created tab. Is it any way to do it from script?
This picture is a screenshot after new sheet creation (Week 1 Lesson 2).
P.S.
I found that I have to reformulate the question. The problem is that I want to do it from another spreadsheet (this spreadsheet has onSubmit trigger and create and place info on other open spreadsheet (leaderboard). I check the setActiveSheet(sheet). It works only with the spreadsheet that runs the script. So I have to send some signal to leaderboard to activate sheet. But I can't understand how to do that
Straight from the developers guide
setActiveSheet(sheet)
As far as I know, there is no way to use Apps Script to change the active sheet on another open spreadsheet not running the script.
You could install a time-based trigger that ran every minute in the leaderboard sheet that checked for a new entry and then changed the active sheet as needed. This isn't instant but would take at most 60 seconds to update.