I got a function to shuffle multiple columns with different amount of values. For every column I have a function that shuffles the values in that column. I Combine all those functions into one function that shuffles all the columns.
function shuffleLevel345() {
shuffleColumnA();
shuffleColumnB();
shuffleColumnC();
shuffleColumnD();
shuffleColumnE();
}
function shuffleColumnA() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("A1:A101");
range.setValues(shuffleArray(range.getValues()));
}
function shuffleColumnB() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("B1:B99");
range.setValues(shuffleArray(range.getValues()));
}
function shuffleColumnC() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("C1:C89");
range.setValues(shuffleArray(range.getValues()));
}
function shuffleColumnD() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("D1:D124");
range.setValues(shuffleArray(range.getValues()));
}
function shuffleColumnE() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var range = sheet.getRange("E1:E75");
range.setValues(shuffleArray(range.getValues()));
}
Can I combine it to one function with all ranges?
I have tried it with:
var ranges = sheet.getRangeList(['A1:A101','B1:B99','C1:C89','D1:D124','E1:E75']);
But that didn't work.
Probably it can be done with a loop this way:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('level_345');
var ranges = ['A1:A101','B1:B99','C1:C89','D1:D124','E1:E75'];
ranges.forEach(r => shuffleRange(r, sheet));
function shuffleRange(r, sheet) {
var range = sheet.getRange(r);
range.setValues(shuffleArray(range.getValues()));
}
Or this way:
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName('level_345');
const ranges = ['A1:A101','B1:B99','C1:C89','D1:D124','E1:E75'];
for (let r of ranges) {
let range = sheet.getRange(r);
range.setValues(shuffleArray(range.getValues()));
}
Related
everyone!
I'm trying to write a script where the cell G3 on the sheet 'ref view' will change to 'Yes' if the cell D9 on 'view' contains 'BP' when I run it. The problem is that the execution is completed without errors but when I try to test it nothing happens.
This is the code:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var sheetInfo = ss.getSheetByName('ref view')
var data = sheet.getDataRange().getValues();
var rangeView = ss.getRange('View!D9');
var cell = ss.getRange('ref view!G3');
var range = rangeView;
for(var i=0; i<data.length; i++) {
if(rangeView == 'BP') {
sheet.getRange(cell).clear().setValue('Yes');
}
}
} ```
Try it this way:
function one() {
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getActiveSheet();
var vs1 = sh1.getDataRange().getValues();
var sh2 = ss.getSheetByName('ref view')
var sh3 = ss.getSheetByName('view');
var sh3D9v = sh3.getRange('D9').getValue();//value
var sh2G3r = sh2.getRange('G3');//Range
for(var i=0; i<vs1.length; i++) {
if(sh3D9v == 'BP') {
sh1.getRange(sh2G3r).setValue('Yes');
}
}
}
Be aware that a range does not return a value without the use of a getValue() or a getValues();
At first I had the error of my rows being out of bounds? Then I get this error with "TypeError: SpreadsheetApp.getActiveSpreadSheet is not a function" after I tampered a bit.
I'm not sure why my script isn't working. I have one function to just autoresize all my rows and another function to resize my rows a specific amount (this one keeps saying my rows are out of bounds) Both of these functions use to work for my rows, so I'm not sure what changed.
function onOpen() {
SpreadsheetApp.getUi().createMenu('Scripts')
.addItem('Hide columns', 'hideCols')
.addItem('Show Columns', 'show')
.addItem('Auto Resize Rows', 'autoResizeRows')
.addItem('Resize Rows', 'resizeRows')
.addToUi()
}
function hideCols() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var cells = sheet.createTextFinder("Pictures").matchEntireCell(true).findAll();
cells.map(x => sheet.hideColumns(x.getColumn()));
}
function show() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var cells = sheet.createTextFinder("Pictures").matchEntireCell(true).findAll();
cells.map(x => sheet.showColumns(x.getColumn()));
}
function autoResizeRows() {
var ss = SpreadsheetApp.getActiveSpreadSheet();
var dataRange = ss.getDataRange();
var lastRow = dataRange.getLastRow();
ss.autoResizeRows(2, lastRow);
}
function resizeRows() {
var sss = SpreadsheetApp.getActiveSpreadsheet();
var cellss = sss.getSheetByName('Sheet1');
var responseData = cellss.getDataRange().getValues();
cellss.setRowHeights(2,responseData.length, 300);
}
Fixed:
function onOpen() {
SpreadsheetApp.getUi().createMenu('Scripts')
.addItem('Hide columns', 'hideCols')
.addItem('Show Columns', 'show')
.addItem('Auto Resize Rows', 'autoResizeRows')
.addItem('Resize Rows', 'resizeRows')
.addToUi()
}
function hideCols() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var cells = sheet.createTextFinder("Pictures").matchEntireCell(true).findAll();
cells.map(x => sheet.hideColumns(x.getColumn()));
}
function show() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
var cells = sheet.createTextFinder("Pictures").matchEntireCell(true).findAll();
cells.map(x => sheet.showColumns(x.getColumn()));
}
function autoResizeRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ssname = ss.getActiveSheet()
var lastRow = ssname.getDataRange().getLastRow();
ssname.autoResizeRows(2, lastRow-2);
}
function resizeRows() {
var sss = SpreadsheetApp.getActiveSpreadsheet();
var cellss = sss.getSheetByName('Sheet1');
var responseData = cellss.getDataRange().getValues();
cellss.setRowHeights(2,responseData.length-2, 300);
}
Because you've a typo in this function:-
function autoResizeRows() {
var ss = SpreadsheetApp.getActiveSpreadSheet();
var ssname = ss.getActiveSheet()
var lastRow = ssname.getLastRow();
ssname.autoResizeRows(1, lastRow);
}
It's getActiveSpreadsheet not getActiveSpreadSheet that's why your code is breaking, change that S to s.
Reference:-
getActiveSpreadsheet
I have the following code but I'm getting the following error after running it.
Is there any way to resolve this?
The data that I'm retrieving has about that amount of cell but it may be retrieving blank rows but all the rows has formulas.
N# of rows per file:
uno: 72850
dos: 72850
tres: 72850
cuatro: 72850
cinco: 72850
Total: 364250
function test() {
var dest = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var ss = SpreadsheetApp.openById('id2')
var sh = ss.getSheetByName('uno')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id2')
var sh = ss.getSheetByName('dos')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id3')
var sh = ss.getSheetByName('tres')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id4')
var sh = ss.getSheetByName('cuatro')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id5')
var sh = ss.getSheetByName('cinco')
var values = sh.getRange('A1:AF'+sh.getLastRow()).getValues()
dest.getRange(dest.getLastRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
};
The error message seems to be related to current Spreadsheets limits in Google Sheets as #Cooper mentioned, currently the limit is the following:
Up to 10 million cells or 18,278 columns (column ZZZ) for spreadsheets that are created in or converted to Google Sheets.
Creating a new sheet could do the work as the limits apply per sheets.
Try to replace getLastRow() by getLastDataRow() after adding
Object.prototype.getLastDataRow = function(col){
var lastRow = this.getLastDataRow();
if (col == null){col='A'}
var range = this.getRange(col + lastRow);
if (range.getValue() !== "") {
return lastRow;
} else {
return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
}
};
so ...
function test() {
var dest = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
var ss = SpreadsheetApp.openById('id2')
var sh = ss.getSheetByName('uno')
var values = sh.getRange('A1:AF'+sh.getLastDataRow()).getValues()
dest.getRange(dest.getLastDataRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id2')
var sh = ss.getSheetByName('dos')
var values = sh.getRange('A1:AF'+sh.getLastDataRow()).getValues()
dest.getRange(dest.getLastDataRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id3')
var sh = ss.getSheetByName('tres')
var values = sh.getRange('A1:AF'+sh.getLastDataRow()).getValues()
dest.getRange(dest.getLastDataRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id4')
var sh = ss.getSheetByName('cuatro')
var values = sh.getRange('A1:AF'+sh.getLastDataRow()).getValues()
dest.getRange(dest.getLastDataRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
var ss = SpreadsheetApp.openById('id5')
var sh = ss.getSheetByName('cinco')
var values = sh.getRange('A1:AF'+sh.getLastDataRow()).getValues()
dest.getRange(dest.getLastDataRow()+1,1,values.length,values[0].length).setValues(values)
SpreadsheetApp.flush();
};
Object.prototype.getLastDataRow = function(col){
var lastRow = this.getLastDataRow();
if (col == null){col='A'}
var range = this.getRange(col + lastRow);
if (range.getValue() !== "") {
return lastRow;
} else {
return range.getNextDataCell(SpreadsheetApp.Direction.UP).getRow();
}
};
I want to copy the range of values from one spreadsheet to another (A1:H45). I tried the code below. In spite of copying the values of Cells A1:H45, its copying A1 Cell data in a new sheet throughout A1:H45, which means the same data getting copied 45 times.
function Copy()
{
var sss = SpreadsheetApp.openById('1IK6YsZS3_NNAlFA41d8uKriPzfZs_2Ukyh94eeBFj40');
var ss = sss.getSheetByName('CURRENT');
var range = ss.getRange('A1:H45');
var data = range.getValues();
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var yourNewSheet = activeSpreadsheet.getSheetByName("New sheet");
if (yourNewSheet != null)
{
activeSpreadsheet.deleteSheet(yourNewSheet);
}
yourNewSheet = activeSpreadsheet.insertSheet();
yourNewSheet.setName("Name of your new sheet");
yourNewSheet.getRange(yourNewSheet.getLastRow()+1,1,45,8).setValue(data);
}
Try this -
function Copy()
{
var sss = SpreadsheetApp.openById('1IK6YsZS3_NNAlFA41d8uKriPzfZs_2Ukyh94eeBFj40');
var ss = sss.getSheetByName('CURRENT');
var range = ss.getRange('A1:H45');
var data = range.getValues();
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var yourNewSheet = activeSpreadsheet.getSheetByName("New sheet");
if (yourNewSheet != null)
{
activeSpreadsheet.deleteSheet(yourNewSheet);
}
yourNewSheet = activeSpreadsheet.insertSheet();
yourNewSheet.setName("Name of your new sheet");
yourNewSheet.getRange(yourNewSheet.getLastRow()+1,1,45,8).setValues(data);
}
Your code is perfect. All you had to do was use setValues (plural) instead of setValue :)
in Google Sheets I have to repeat a function because getSheetByName() does not accept an array of sheets, it only accepts one sheet.
Is there a way to have one function that loops through specified sheets (not all sheets)?
i.e.
("Sheet1", "Sheet2" ) etc.
function recordHistory_1() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var source = sheet.getRange("A2:B2");
var values = source.getValues();
values[0][0] = new Date();
sheet.appendRow(values[0]);
};
function recordHistory_2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet2");
var source = sheet.getRange("A2:B2");
var values = source.getValues();
values[0][0] = new Date();
sheet.appendRow(values[0]);
};
Reason I'm asking is because I have over 20 sheets, and so I have to write this function 20 times...
You can generalize / reuse the same function for 20 different calls as follows.
var sheetListArray = ["Sheet1", "Sheet2", "Sheet3"......"Sheet20"]
var ss = SpreadsheetApp.getActiveSpreadsheet();
for( var i = 1 ; i <= sheetListArray.length ; i++)
// You can call the below function 20 time with different Sheet name each time.
recordHistory(sheetListArray[i]);
}
function recordHistory(sheetName) {
var sheet = ss.getSheetByName(sheetName);
var source = sheet.getRange("A2:B2");
var values = source.getValues();
values[0][0] = new Date();
sheet.appendRow(values[0]);
}
You can also call the function once and loop through an array of sheetnames inside the function.
function recordHistory() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
["Sheet1", "Sheet2", "Sheet3"].forEach(function (s) {
var sheet = ss.getSheetByName(s);
var values = sheet.getRange('A2:B2').getValues()
values[0][0] = new Date();
sheet.appendRow(values[0]);
})
}