Run script in specified sheet in Google Sheets - google-apps-script

I have made a speadsheet with Google Sheet which contains two sheets:
Name of sheet 1: Start
Name of sheet 2: Playlist
I have made the following script:
function shuffleSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var range = sheet.getRange("A4:C15");
// Randomizes the range
range.randomize();
}
The script worked fine when I only had 1 sheet. Now I have two and I want the script to run on the sheet named Playlist.
I can't figure out how to do this. Please help.
Thanks.

If you only want to run the code on the "Playlist" sheet this should work for you.
function shuffleSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Playlist");
var range = sheet.getRange("A4:C15");
// Randomizes the range
range.randomize();
}
I you want to run it on all sheets then you need to use a for loop.
function shuffleSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
for (var i = 0; i < sheets.length; i++) {
var sheet = sheets[i];
var range = sheet.getRange("A4:C15");
// Randomizes the range
range.randomize();
}
}

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()

google sheets copy formatting from active sheet to every other sheet

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)

Output an array to the first sheet

Id like to output the list of sheet names obtained from this loop, to the first sheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetNameArray = [];
var sheets = ss.getSheets();
for (var i = 0; i < sheets.length; i++) {
sheetNameArray.push(sheets[i]);
}
Is this what you want to do? This script imports sheet names in the opened spreadsheet to the current sheet.
Flow :
Retrieve sheets from the opened spreadsheet.
Create 2 dimensional array to using setValues().
Import the created data to the current sheet.
Sample script :
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheetNameArray = sheets.map(function(e){return [e.getName()]});
ss.getActiveSheet().getRange(1, 1, sheetNameArray.length, sheetNameArray[0].length).setValues(sheetNameArray);
Note :
If you want to import the sheet name to a row, please use this var sheetNameArray = [sheets.map(function(e){return e.getName()})]; instead of var sheetNameArray = sheets.map(function(e){return [e.getName()]});
Reference :
setValues()
If I misunderstand your question, I'm sorry.

Get a list of all the sheet tab names from a Google spreadsheet and put the names into another spreadsheet

I have a google spreadsheet that contains around 50 to 60 sheets. I am trying to get the names of all those sheets, and put them into another spreadsheet.
So far I have written the following code
function myfunction(){
var ss = SpreadsheetApp.openById("abck12345");
var sheetCount = ss.getSheets().length;
var i=0;
var sheetlist;
while(i<sheetCount){
sheetlist = ss.getSheets()[i];
Logger.log(sheetlist);
}
}
While executing this i am getting an error "Exceeded maximum execution time"
It is more efficient to get all the sheets at once. Your code is getting all the sheets on every loop. This code gets all the sheet names and puts them into a new spreadsheet:
function myfunction() {
var allSheets,i,sheetCount,sheetlist,sheetName,ss,
targetSh,targetSS,thisSheet;
ss = SpreadsheetApp.openById("abck12345");
targetSS = SpreadsheetApp.openById("999999");
allSheets = ss.getSheets();
sheetCount = allSheets.length;
sheetlist = [];
for (i=0;i<sheetCount;i++) {
thisSheet = allSheets[i];
sheetName = thisSheet.getName();
sheetlist.push([sheetName]);//Put an inner array of each sheet tab name into
//an outer array named sheetlist
//Creates a 2D array for setting the values later
}
Logger.log(sheetlist);
targetSh = targetSS.getSheetByName('target sheet tab name');
targetSh.getRange(targetSh.getLastRow()+1, 1, sheetlist.length, sheetlist[0].length).setValues(sheetlist);
}
You are not incrementing i. A better way to do would be as follows:
function myfunction(){
var ss = SpreadsheetApp.openById("1cmUWU_nbE6idJUmpNtKnhq0KUekVXnW9OYVZr93TSNs");
var sheets = ss.getSheets();
var i;
for(i in sheets){
Logger.log(sheets[i].getName())
}
}

Auto Fill Cell Script

I have a sheet with two columns. A Date and Assist.
The sheet is filled out with a Date and with a number of how many assist.
What I would like is for the Assist column to automatically insert a 0 if there is a date entered and only if there is no number already in the Assist column.
I have the following script so far and it is triggered onOpen.
function storeValue() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// ss is now the spreadsheet the script is associated with
var sheet = ss.getSheets()[0]; // sheets are counted starting from 0
// sheet is the first worksheet in the spreadsheet
var cell = sheet.getRange("J3:J");
cell.setValue(0);
}`
This is a little slow, but works. Adapt as needed. It currently assumes your data are in columns A & B.
function storeValue() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// ss is now the spreadsheet the script is associated with
var sheet = ss.getSheets()[0]; // sheets are counted starting from 0
var dataRange = sheet.getRange("A:B")
for (var row = 1; dataRange.getNumRows(); row++) {
if(sheet.getRange(row,1).getValue() != ''){
if(sheet.getRange(row,2).getValue() == ''){
sheet.getRange(row,2).setValue(0);
}
}
}
}