google sheets copy formatting from active sheet to every other sheet - google-apps-script

I really need your help. After 2 days, I'm still on "ZERO".
I need a script that copies all the formats from an active sheet to every other sheet.
It would be nice to have the conditional formatting, alternating colors and data validation too.
I think it's not so hard, but I'm really new in script writing.
This is the sheet:
https://docs.google.com/spreadsheets/d/1mOkm_r4rngPXTkIerSQJKxRih31sM7XoZCZVnoHUc5M/edit?usp=sharing
The code so far:
enter function copyFormatting(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var copyTo = ss.getSheetByName('agent2');
var range = copyTo.getRange(1, 1, copyTo.getMaxColumns(), copyTo.getMaxRows());
sheet.getBandings()[0].copyTo(copyTo.getRange(1,1)),SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
}
function allcopyFormatting() {
var spreadsheet = SpreadsheetApp.getActive();
var allSheets = spreadsheet.getSheets();
allSheets.forEach(function(sheet){
if(sheet.getSheetName() !== "detroit"){ // if you dont want to use the formatting on this sheet
sheet.activate();
copyFormatting(); // the function what yo like to run
}
})
}
I was able to figure out and write some functions: clear formatting on all sheets, format row headers on all sheets, show a list of sheets, but this one is too hard for me.
Even if I try to model it first with the macro recorder.
I would be very grateful, if you could help me with this issue.

I was able to figure it out:
function allcopyFormatting() {
var spreadsheet = SpreadsheetApp.getActive();
var allSheets = spreadsheet.getSheets();
var sampleSheet = spreadsheet.getActiveSheet(); // from the spreadsheet select the active sheet
var maxRow =sampleSheet.getMaxRows();
var maxCol =sampleSheet.getMaxColumns();
var sampleRange = sampleSheet.getRange(1,1,maxRow,maxCol);
allSheets.forEach(function(sheet){
if(sheet.getSheetName() !== "detroit"){ // if you dont want to use the formatting on this sheet
sheet.activate();
var targetSheet = spreadsheet.getActiveSheet(); // from the spreadsheet select the active sheet
var targetRange = targetSheet.getRange(1,1);
sampleRange.copyTo(targetRange, {formatOnly:true}) // it copies the formatting
}
})
}
Reference
Copy To

Try insertSheet(options) and if that doesn't work the try copyTo(destination, options)

Related

Is it possible to insert cells into another sheet in Google Sheets through script

I have a Google-Sheet script that creates few cells and inserts them into active sheet. I would like to ask someone for help. I would need the script to do exactly same work, but only change I would need is to pick the sheet where the cells will be added. At this moment it always applies to the active sheet.
As you may recognized I am absolutely noob in coding and also in English, so I really appriciate everyone who tries to help me with this.
Thank you very much, Petr
The code I have right now:
function myButton(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A3:D3");
range.insertCells(SpreadsheetApp.Dimension.ROWS);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = ss.getActiveRange();
var R = range.getRowIndex();
}
How about the following modification patterns?
Pattern 1:
When you want to select the specific sheet in the active Spreadsheet, you can modify your script as follows.
function myButton() {
var sheetName = "Sheet2"; // Added. Please set the sheet name you want to select.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName); // Modified
var range = sheet.getRange("A3:D3");
range.insertCells(SpreadsheetApp.Dimension.ROWS);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = ss.getActiveRange();
var R = range.getRowIndex();
}
Pattern 2:
When you want to select the specific sheet in the other Spreadsheet from the active Spreadsheet, you can modify your script as follows.
function myButton() {
var spreadsheetId = "###"; // Added. Please set the spreadsheet ID you want to use.
var sheetName = "Sheet2"; // Added. Please set the sheet name you want to select.
var ss = SpreadsheetApp.openById(spreadsheetId); // Modified
var sheet = ss.getSheetByName(sheetName); // Modified
var range = sheet.getRange("A3:D3");
range.insertCells(SpreadsheetApp.Dimension.ROWS);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = ss.getActiveRange();
var R = range.getRowIndex();
}
Note:
In this answer, from I would need the script to do exactly same work, but only change I would need is to pick the sheet where the cells will be added. At this moment it always applies to the active sheet., it supposes that your script works fine for the 1st tab in the active Spreadsheet. Please be careful this.
When getSheets() is used, for example, in the case of var sheet = ss.getSheets()[0];, it's the 1st tab. In the case of var sheet = ss.getSheets()[1];, it's the 2nd tab.
References:
getSheetByName(name)
openById(id)
getSheets()

how to copy from one google sheet to another with button

i have made this in excel but cannot use it online. so i made it in google sheets but it does not work. What i want to do:
data ( values only, no formats) from sheet Tmrw range C3:Y20 copy to sheet Today range C3:y20, and after that clear data from sheet Tmrw range C3:Y20
i used this code:
function moveValuesOnly () {
var ss = SpreadsheetApp.getActiveSpreadsheet ();
var source = ss.getRange ("Tmrw"!C3:Y20");
var destSheet = ss.getSheetByName("Today");
var destRange = destSheet.getRange("Today!C3:Y20");
source.copyTo (destRange,"Today!C3:Y20);
source.clear ();
}
it does not work. i am not surprised.
the file is here:
https://docs.google.com/spreadsheets/d/11uyaRe7khP9PywXKkEh5e5xWLvm68Nw58R6dHkaTxvk/edit?usp=sharing
Modification points:
When getRange of Class Spreadsheet, you can include the sheet name to the A1Notation.
In this case, ss.getRange("'Today'!C3:Y20") can be used instead of ss.getSheetByName("Today");.
"Tmrw"!C3:Y20" and "Today!C3:Y20 are not enclosed by ". In this case, please modify to "Tmrw!C3:Y20" and "'Tmrw'!C3:Y20", and "Today!C3:Y20" and "'Today'!C3:Y20".
In your situation, I thought that the arguments of copyTo of Class Range is copyTo(destination).
When above points are reflected to your script, it becomes as follows.
Modified script:
function moveValuesOnly() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getRange("'Tmrw'!C3:Y20");
var destRange = ss.getRange("'Today'!C3:Y20");
source.copyTo(destRange);
source.clear();
}
Note:
When you want to use getSheetByName, you can also modify your script as follows.
function moveValuesOnly() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = ss.getSheetByName("Tmrw");
var source = srcSheet.getRange("C3:Y20");
var dstSheet = ss.getSheetByName("Today");
var destRange = dstSheet.getRange("C3:Y20");
source.copyTo(destRange);
source.clear();
}
References:
A1 notation
getRange(a1Notation) of Class Spreadsheet
copyTo(destination) of Class Range
getSheetByName(name)

Google Sheets Script hideSheet() , Sheet not hidden

Trying to duplicate a hidden template, causing it to be revealed, but then have it be hidden again after the duplication.
The hideSheet() function is not working. Any ideas?
function googleQuiz(){
//Duplicate Sheet
var ss =SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.setActiveSheet(ss.getSheetByName('QuizFormTPlate'));
ss.duplicateActiveSheet();
sheet.hideSheet();
//Rename sheet
var dSheet = ss.setActiveSheet(ss.getSheetByName('Copy of QuizFormTPlate'));
var date = new Date();
var tz = ss.getSpreadsheetTimeZone();
var marksDate = Utilities.formatDate(date, tz, 'dd-MMM');
var name = "G-Quiz ".concat(marksDate);
dSheet.setName(name);
//Insert Cell
var cell = dSheet.getRange("C2");
cell.setValue('Formative');
}
Actually it's "showSheet()"
This works for me:
var spreadsheet = SpreadsheetApp.getActive();
var sheetTemplate = spreadsheet.getSheetByName('Template');
sheetTemplate.showSheet();
//do something...
sheetTemplate.hideSheet();
Since hideSheet() takes no effect if the sheet is already hidden:
https://developers.google.com/apps-script/reference/spreadsheet/sheet#hidesheet
Showing the hidden sheet first should do the trick:
https://developers.google.com/apps-script/reference/spreadsheet/sheet#showsheet
Here is alternative solution that will that copy the sheet and keep it hidden
function googleQuiz(){
//Duplicate Sheet
var ss =SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('QuizFormTPlate');
var dSheet = sheet.copyTo(SpreadsheetApp.openById(ss.getId()))
dSheet.showSheet()
//Rename sheet
var date = new Date();
var tz = ss.getSpreadsheetTimeZone();
var marksDate = Utilities.formatDate(date, tz, 'dd-MMM');
var name = "G-Quiz ".concat(marksDate);
dSheet.setName(name);
//Insert Cell
var cell = dSheet.getRange("C2");
cell.setValue('Formative');
}
Basically, instead of your using duplicateActiveSheet() function of a spreadsheet object. You can use copyTo() function of a sheet object and provide it with the current spreadsheet.
sheet.copyTo(SpreadsheetApp.openById(ss.getId())).
And directly pass the new sheet object to var dSheet to make it visible and rename it.
It appears the issue is caused by using setActiveSpreadSheet on a hidden sheet, if you call ss.getSheetByName('QuizFormTPlate').showSheet(); prior to the ss.setActiveSpreadsheet(ss.getSheetByName('QuizFormTPlate')); the call to hideSheet() will work fine afterwards. Posting this here so anyone else running into this problem can just add the .showSheet() instead of reworking their whole code.

How to get number of sheet from list

What script should do. First, get sheet names from range on sheet List_1. Then, using this array of names, put some data only on these sheets.
https://docs.google.com/spreadsheets/d/1Xdws6Vr6YVXuj19mvpOvkpJokOfY2T7Sh1aMpous_Xw/edit#gid=0
function VList() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheetNames = ss.getSheetByName("List_1");
var rangeNames = sheetNames.getRange("B3:B5").getValues();
for (var sheetId = 0; sheetId<sheets.length; sheetId++){
if(sheets[sheetId].getSheetName() == rangeNames) {
ss.getRange("C1").setValue("done");
}
}
}
Problem. When script starts - nothing happens. How to get number of sheet and vlookup only on them? Maybe, exist some function like "getSheetId" which can get number of sheet.
Solved, thanks to bruce.
function VList() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheetNames = ss.getSheetByName("List_1").getRange("B3:B5").getValues();
sheetNames.forEach(function(row) {
var sheet = ss.getSheetByName (row[0]);
if (!sheet) throw 'missing sheet ' + row[0];
sheet.getRange("c1").setValue ("done");
});
}
getValues() returns a two dimensional array, which you then compare with a string value - so that will never be true.
Once you fix that, you will still be updating the same cell, C1 with 'done' multiple times - ss points to the List_1 sheet - I assume you mean to update the sheet whose name is in the list.
Why not just use the list in b3:b5 to simply get each sheet by name and update it? There's no need to go through the sheets list since you already know their names.
something like
ss.getSheetByName("List_1").getRange("b3:b5").getValues()
.forEach(function(row) {
var sheet = ss.getSheetByName (row[0]);
if (!sheet) throw 'missing sheet ' + row[0];
sheet.getRange("c1").setValue ("done");
});

How to set trigger 'onOpen' on a spreadsheet instead of a sheet? (Focus last row)

I want to focus the last row on a specific sheet (or if possible on every one of them) so here is my function triggered by onOpen on the sheet:
function FocusLastRows() {
var spsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spsheet.getSheetByName("Usuelle_2013");
var rowInt = sheet.getLastRow();
var colInt = sheet.getLastColumn();
var range = sheet.getRange(rowInt, colInt)
sheet.setActiveRange(range)
}
This event works but if my first loaded sheet is different from the one I want it to focus on it just teleports to the other sheet. Best will be to set an .onOpen on the spreadsheet.
Is that possible?
I found a good alternative for it. It's custom menu with function link to it.
//Pierre-luc Des. Script
//Build your menu
function setMenu() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menubuttons = [ {name: "Dernière ligne usuelle", functionName: "GOTOLastRow}];
ss.addMenu("Outils", menubuttons);
}
function GOTOLastRow() {
var spsheet = SpreadsheetApp.getActiveSpreadsheet();
var currentTime = new Date();
var sheet = spsheet.getSheetByName("Usuelle_" + currentTime.getFullYear());
var rowInt = sheet.getLastRow();
var colInt = sheet.getLastColumn();
var range = sheet.getRange(rowInt, colInt);
sheet.setActiveRange(range);
}
Now i will have a clickable menu "Outils" which will let me do the function.
This is rather inelegant, but perhaps try just checking whether the activated sheet is the target sheet? Something like this:
function FocusLastRows() {
var spsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spsheet.getSheetByName("Usuelle_2013");
if (sheet == spsheet) {
var rowInt = sheet.getLastRow();
var colInt = sheet.getLastColumn();
var range = sheet.getRange(rowInt, colInt);
sheet.setActiveRange(range);
}
}
The Google Apps Script documentation seems to indicate that you'll need custom libraries to get any other trigger events than the ones provided, and I'm not sure if it's even in-principle possible to trigger only when a certain specific sheet is activated.
I figure checking the sheet with if comparisons is the next-simplest solution.