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.
Related
How can I merge multiple tabs in a Google Spreadsheet using Google App Script ? Data in all the tabs keep changing.
For example, I have 'Sheet1', 'Sheet2' and 'Sheet3' in a Google spreadsheet. Data in all these sheets have 3 columns - Name and email ID & Region. Now, I want to merge/combine the data present in these 3 sheets/tabs into a 4th Sheet i.e. 'Sheet 4' having same Columns (name, email Id & Region). The 4th sheet should have data as - data of Sheet1 followed by Sheet2 and then Sheet3. The number of rows in all the 3 sheets keeps changing.
=query({Sheet1!A1:C; Sheet2!A1:C; Sheet3!A1:C}, "where Col1 is not null", 0)
I wouldn't use a script for this; the worksheet formulas are much faster, at least most of the time.
Make sure you use semicolons to separate the ranges. Semicolons are the End_Of_Row operator for array literals.
If you really want to use a script...
function combineSheets() {
var sApp = SpreadsheetApp.getActiveSpreadsheet();
var s1= sApp.getSheetByName("Sheet1");
var s2= sApp.getSheetByName("Sheet2");
var s3= sApp.getSheetByName("Sheet3");
var s4= sApp.getSheetByName("Sheet4");
// If Sheet4 doesn't exist you'll need to create it here.
var s1values = s1.getRange(1,1,s1.getLastRow(),3).getValues();
var s2values = s2.getRange(1,1,s2.getLastRow(),3).getValues();
var s3values = s3.getRange(1,1,s3.getLastRow(),3).getValues();
// Now, we can put out all together and stuff it in Sheet4
var s4values = [];
s4values = s1values.concat(s2values,s3values);
s4.getRange(1,1,s4values.length,3).setValues(s4values);
}
You don't need a script for this. In the fourth sheet, enter the following formula in A2:
={filter(Sheet1!A2:C, len(Sheet1!A2:A)); filter(Sheet2!A2:C, len(Sheet2!A2:A)); filter(Sheet3!A2:C, len(Sheet3!A2:A))}
It returns the contents of columns A-C where the entry in column A is nonempty, and stacks them in one array.
You can use Google Apps Scripts for this.
var ss = SpreadsheetApp.getActiveSpreadsheet();
function TotalsSheet() {
var totaldata = [];
var sheets = ss.getSheets();
var totalSheets = 2;
for (var i=0; i < totalSheets; i++) {
var sheet = sheets[i];
var range = sheet.getDataRange();
var values = range.getValues();
for (var row in values) {
totaldata.push(values[row]);
}
}
return totaldata;
}
function Start() {
var All = ss.insertSheet("All-Values");
var totaldata = TotalsSheet();
for (var i = 0; i < totaldata.length; i++) {
All.appendRow(totaldata[i]);
}
}
//This Script is must faster
function consolidateData(){
// defined all variables
var sheetNames = [];
var dataSheet = [];
var dataValues = [];
var conso=[];
var header = [["Name","email ID","Region"]];
var ws = SpreadsheetApp.getActiveSpreadsheet();
// get all sheets
var allsheets = ws.getSheets();
for(var s in allsheets){
var sheet = allsheets[s];
sheetNames[s] = sheet.getName();
dataSheet[s] = ws.getSheetByName(sheetNames[s]);
// adding all sheet's data to a single variable
conso = conso.concat(dataSheet[s].getRange("A2:C"+dataSheet[s].getLastRow()).getValues());
}
// writing data into new sheet
var newSheet = ws.insertSheet().setName("Consolidated_Data");
newSheet.getRange("A1:C1").setValues(header);
newSheet.getRange("A2:C"+(conso.length+1)).setValues(conso);
}
I made one that will do nine tabs' consolidation, with more rows and columns.
=filter({{Sheet1!A1:Q500};{Sheet2!A1:Q500};{Sheet3!A1:Q500};{Sheet4!A1:Q500};{Sheet5!A1:Q500};{Sheet6!A1:Q500};{Sheet7!A1:Q500};{Sheet8!A1:Q500};{Sheet9!A1:Q500}},{{Sheet1!A1:A500};{Sheet2!A1:A500};{Sheet3!A1:A500};{Sheet4!A1:A500};{Sheet5!A1:A500};{Sheet6!A1:A500};{Sheet7!A1:A500};{Sheet8!A1:A500};{Sheet9!A1:A500}}<>"")
I'm wondering if it is a way to generalize the initial filter formula so that you can cut and paste in more sheet numbers, so for instance if there were 20 or 30 tabs with 25 columns, could you consolidate that easily into one page?
How can I merge multiple tabs in a Google Spreadsheet using Google App Script ? Data in all the tabs keep changing.
For example, I have 'Sheet1', 'Sheet2' and 'Sheet3' in a Google spreadsheet. Data in all these sheets have 3 columns - Name and email ID & Region. Now, I want to merge/combine the data present in these 3 sheets/tabs into a 4th Sheet i.e. 'Sheet 4' having same Columns (name, email Id & Region). The 4th sheet should have data as - data of Sheet1 followed by Sheet2 and then Sheet3. The number of rows in all the 3 sheets keeps changing.
=query({Sheet1!A1:C; Sheet2!A1:C; Sheet3!A1:C}, "where Col1 is not null", 0)
I wouldn't use a script for this; the worksheet formulas are much faster, at least most of the time.
Make sure you use semicolons to separate the ranges. Semicolons are the End_Of_Row operator for array literals.
If you really want to use a script...
function combineSheets() {
var sApp = SpreadsheetApp.getActiveSpreadsheet();
var s1= sApp.getSheetByName("Sheet1");
var s2= sApp.getSheetByName("Sheet2");
var s3= sApp.getSheetByName("Sheet3");
var s4= sApp.getSheetByName("Sheet4");
// If Sheet4 doesn't exist you'll need to create it here.
var s1values = s1.getRange(1,1,s1.getLastRow(),3).getValues();
var s2values = s2.getRange(1,1,s2.getLastRow(),3).getValues();
var s3values = s3.getRange(1,1,s3.getLastRow(),3).getValues();
// Now, we can put out all together and stuff it in Sheet4
var s4values = [];
s4values = s1values.concat(s2values,s3values);
s4.getRange(1,1,s4values.length,3).setValues(s4values);
}
You don't need a script for this. In the fourth sheet, enter the following formula in A2:
={filter(Sheet1!A2:C, len(Sheet1!A2:A)); filter(Sheet2!A2:C, len(Sheet2!A2:A)); filter(Sheet3!A2:C, len(Sheet3!A2:A))}
It returns the contents of columns A-C where the entry in column A is nonempty, and stacks them in one array.
You can use Google Apps Scripts for this.
var ss = SpreadsheetApp.getActiveSpreadsheet();
function TotalsSheet() {
var totaldata = [];
var sheets = ss.getSheets();
var totalSheets = 2;
for (var i=0; i < totalSheets; i++) {
var sheet = sheets[i];
var range = sheet.getDataRange();
var values = range.getValues();
for (var row in values) {
totaldata.push(values[row]);
}
}
return totaldata;
}
function Start() {
var All = ss.insertSheet("All-Values");
var totaldata = TotalsSheet();
for (var i = 0; i < totaldata.length; i++) {
All.appendRow(totaldata[i]);
}
}
//This Script is must faster
function consolidateData(){
// defined all variables
var sheetNames = [];
var dataSheet = [];
var dataValues = [];
var conso=[];
var header = [["Name","email ID","Region"]];
var ws = SpreadsheetApp.getActiveSpreadsheet();
// get all sheets
var allsheets = ws.getSheets();
for(var s in allsheets){
var sheet = allsheets[s];
sheetNames[s] = sheet.getName();
dataSheet[s] = ws.getSheetByName(sheetNames[s]);
// adding all sheet's data to a single variable
conso = conso.concat(dataSheet[s].getRange("A2:C"+dataSheet[s].getLastRow()).getValues());
}
// writing data into new sheet
var newSheet = ws.insertSheet().setName("Consolidated_Data");
newSheet.getRange("A1:C1").setValues(header);
newSheet.getRange("A2:C"+(conso.length+1)).setValues(conso);
}
I made one that will do nine tabs' consolidation, with more rows and columns.
=filter({{Sheet1!A1:Q500};{Sheet2!A1:Q500};{Sheet3!A1:Q500};{Sheet4!A1:Q500};{Sheet5!A1:Q500};{Sheet6!A1:Q500};{Sheet7!A1:Q500};{Sheet8!A1:Q500};{Sheet9!A1:Q500}},{{Sheet1!A1:A500};{Sheet2!A1:A500};{Sheet3!A1:A500};{Sheet4!A1:A500};{Sheet5!A1:A500};{Sheet6!A1:A500};{Sheet7!A1:A500};{Sheet8!A1:A500};{Sheet9!A1:A500}}<>"")
I'm wondering if it is a way to generalize the initial filter formula so that you can cut and paste in more sheet numbers, so for instance if there were 20 or 30 tabs with 25 columns, could you consolidate that easily into one page?
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();
}
}
Is there a function to reorder the tabs in Google Sheets? We'd like to arrange them on a specific value obtained on each of them,
/** ... */
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Store all the worksheets in this array
var sheetNameArray = [];
var sheets = ss.getSheets();
for (var i = 0; i < sheets.length; i++) {
z = sheets[i].getRange(3, 5);
z = z.getValue();
sheetNameArray.push(z);
}
/** Use value in array to reorder sheets */
You want to reorder the sheets in the opened spreadsheet using the sheet names in sheetNameArray. If my understanding is correct, how about this sample script? This sample script reorders sheets in the opened spreadsheet using moveActiveSheet().
Sample script :
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetNameArray = [];
var sheets = ss.getSheets();
for (var i = 0; i < sheets.length; i++) {
z = sheets[i].getRange(3, 5);
z = z.getValue();
sheetNameArray.push(z);
}
sheetNameArray.forEach(function(e, i){
ss.setActiveSheet(ss.getSheetByName(e));
ss.moveActiveSheet(i+1);
});
Note :
It supposes that your script can retrieve the sheet names to sheetNameArray.
The sheet names in sheetNameArray are required to be included in the opened spreadsheet.
The length of sheetNameArray is required to be the same with the length of var sheets = ss.getSheets();.
Reference :
moveActiveSheet()
If I misunderstand your question, I'm sorry.
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())
}
}