Google Sheets - Unmerge cells and fill down - google-sheets

I need to take a sheet maintained by someone else and do the following (so that I can export to a csv):
unmerge all cells
fill values down
merged cells are in multiple columns, so I need to iterate over a range
It's too much to do it by hand, and it will need done periodically. My javascript and google sheets object model knowledge approximate zero, but I know it's possible because I could do it in VBA. I searched but can only find programmatic answers for VBA/Excel.
How can I do this efficiently in Google Sheets?

You can use the breakapart() class to do this. I am assuming that the merged range is not static and has multiple occurrences. This script will unmerge all merged ranges in the active sheet.
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).breakApart();
}

Adapted from #lreeder's answer
The following breaks and fill blank with above on the selected range:
function onOpen() {
var ui = SpreadsheetApp.getUi();
// Or DocumentApp or FormApp.
ui.createMenu('BreakAndFill')
.addItem('Break Fill Blank Cells', 'menuItem1')
.addToUi();
}
function menuItem1() {
BreakandfillBlankWithAbove()
}
//Breaks range
//Iterates over the range from top to bottom
//and left to right, and fills blank cells
//with the value right above them.
function BreakandfillBlankWithAbove() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveRange();
Logger.log('Range height is: ' + range.getHeight());
var values = range.getValues();
range.breakApart();
var width = values[0].length;
var height = values.length;
Logger.log('Width is ' + width);
Logger.log('Height is ' + height);
for (var i = 0; i < width; i++) {
var lastVal = '';
for(var j = 0; j < height; j++) {
var currValue = values[j][i];
var dataType = typeof(currValue);
//Empty string check returns true if dataType
//is a number, and value is zero. In that case
//we don't want to overwrite the zero, so only
//check emptyString for string types.
if(currValue === undefined ||
(dataType === 'string' && currValue == '')
) {
//getCell parameters are relative to the current range
//and ones based
var cell = range.getCell(j+1, i+1);
cell.setValue(lastVal);
}
else {
lastVal = currValue;
}
}
}
SpreadsheetApp.flush();
}

Related

Summing Values of Cells of Certain Font Color in Google Sheets

I have a table with cells that contain numbers styled with fonts of different colors. I would like to sum all cell values of a certain color and output that sum into a separate cell. How can I do that?
Try (B1 contains a checkbox)
=sumColor(A:A,B1)
put the formula in a range with the same color as the data to be added
add a check box to allow you updating the value when colors will be changed
the formula can be located in another sheet
with this custom function
function sumColor(range) {
var r = SpreadsheetApp.getActiveRange();
var color = r.getFontColors();
var total = 0;
var addresses = r.getFormula().match(/(?<=\().*(?=\))/g)[0].split(/[;|,]/)
for (var i = 0; i < addresses.length - 1 ; i++) {
try {
var sh = SpreadsheetApp.getSheetByName(addresses[i].split('!')[0].replace("'", ""));
var address = addresses[i].split('!')[1].trim();
}
catch (e) {
var sh = SpreadsheetApp.getActiveSheet();
var address = addresses[i].trim()
}
var colors = sh.getRange(address).getFontColors();
var values = sh.getRange(address).getValues();
for (var j = 0; j < colors.length; j++)
for (var k = 0; k < colors[i].length; k++)
if (colors[j][k] == color)
if ((typeof values[j][k]) == 'number')
total += values[j][k];
}
return total;
};

How do I find and delete duplicate values in a range of cells while keeping the first occurrence of a duplicated value in Google Sheets?

Ideally, I want to be able to search through an entire sheet (or range of cells) and remove any values that have been repeated in that sheet (for example, if "2" appears in A3, B8, and D4, then I want to keep it in A3 and delete it in B8 and D4).
I normally see this problem addressed by looking at one column or row for duplicates (using the UNIQUE function) but not for an entire sheet.
How can I do this?
Here's some code that will do that for an entire sheet.
Please note it will work on text and numbers and it will overwrite any formulas.
function removeDuplicates() {
var sheet = SpreadsheetApp.getActive().getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
var traversedValues = [];
var rowCount = values.length;
var colCount = values[0].length;
for (var row = 0; row < rowCount; ++row) {
for (var col = 0; col < colCount; ++col) {
var value = values[row][col];
if (traversedValues.indexOf(value) > -1) {
values[row][col] = null;
} else {
traversedValues.push(value);
}
}
}
range.setValues(values);
}
References for you
Beginner's guide to coding with Google Apps Script
For loops
if...else statements
Arrays

Add Rows and copy formulas from Row before - Timeout

I have a large sheet with over 4000 rows and 30 columns.
I am trying to automaticly add new rows to the sheet if there are less than x "empty" rows. Empty rows are defined by just containing formulas but the data column A is empty.
To check if a row is empty or not I check column A, because you have to enter data in column A. After adding new rows the script should copy/paste the last row before over the new rows, so they contain the formulas as well.
Here is the script (PREVIOUS VERSION):
function addRowsItems() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName('items');
var freeRows = 300; // Number of empty Rows after last Entry
var lRow = sh.getLastRow(), lCol = sh.getLastColumn(), range = sh.getRange(lRow,1,1,lCol);
var startRow = lRow-freeRows;
if(startRow < 0) {
startRow = 1; }
var values = sh.getRange("A" + startRow + ":A").getValues();
var maxIndex = values.reduce(function(maxIndex, row, index) {
return row[0] === "" ? maxIndex : index;
}, 0);
var maxIndex = maxIndex + startRow;
var space = lRow - maxIndex;
var addLines = freeRows - space;
if(space < freeRows) {
sh.insertRowsAfter(lRow, addLines);
range.copyTo(sh.getRange(lRow+1, 1, addLines, lCol), {contentsOnly:false});
}
}
This is working in a new sheet with less data.
But using it in the main sheet with over 4000 rows of data I get a time out. The script adds the new rows but it times out before it is able to copy/paste.
Why is it taking so much time? Is there an easier way to achieve that?
So this is the actual version. Cell B1 contains COUNBLANK of Range A:A.
function addRowsItems() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName('items');
var freeRows = 307; // Number of empty Rows after last Entry
var lRow = sh.getMaxRows(), lCol = sh.getMaxColumns(), range = sh.getRange(lRow,1,1,lCol);
var space = sh.getRange("B1").getValues();
var addLines = freeRows - space;
if(space < freeRows) {
sh.insertRowsAfter(lRow, addLines);
range.copyTo(sh.getRange(lRow+1, 1, addLines, lCol), {contentsOnly:false});
}
}
Try the COUNTBLANK function returns a count of empty cells in a range. Cells that contain text, numbers, errors, etc. are not counted. Formulas that return empty text are counted.

Google Sheets: Change background color of cell using content of another cell

I have a google sheet as above where I am typing in colour codes in a hex format and then naming them. I would like to automatically update the Colour Block column's background colour with the hex code in the code column.
Script I have tried, but setBackground function doesn't work.
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var cells = sheet.getDataRange().getValues();
for(n = 1; n < cells.length; n++) {
var cell = cells[n];
cell[n][2].setBackground(cell[n][1]);
}
}
I did manage to make this work with the following:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var values = range.getValues();
for(r = 1; r < values.length; r++) {
var row = values[r];
var code = row[1];
range.getCell(r+1,3).setBackground(code);
}
}

How to Count and return the Values

I am facing a problem in the following script. I am not much into scripting at all and this is not my script also but here am getting the result which is grouping the values( For Example if i have a value A in three cells it should return the value as 3 instead it is returning AAA. Can someone help me out to count the values and return it
Thanks in Advance,
Here is the script :
function sumBackgroundColors(rangeString, color) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet();
var sumRange = s.getRange(rangeString);
//var sum = 0;
var openCount = 0;
var sumRangeBackground = sumRange.getBackgroundColors();
var sumRangeValues = sumRange.getValues();
for(var row = 0; row < sumRangeBackground.length; row++ ) {
for(var col = 0; col < sumRangeBackground[0].length; col++ ) {
if( sumRangeValues[row][col]=="LG M"&& sumRangeBackground[row][col] == color ) {
openCount = openCount + sumRangeValues[row][col];
//if(sumRangeBackground[row][col] == color && sumRangeValues[row][col] == 1 ) {
// sum = sum + parseFloat(sumRangeValues[row][col]);
}
}
}
return openCount;
//return sum;
}
Here is a function which will take the value to be searched as argument. Rest of the things have been explained in comment lines.
function searchCount(value){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Get the whole data from activesheet in a 2D array
var data = sheet.getDataRange().getValues();
//Initialize counter
var count = 0;
//Iterate through the array
for(var i in data){
for(var j in data[i]){
// if a match found, increament the counter
if(value.toString == data[i][j]){
count++;
}
}
}
// return the count value
return count;
}
your problem could be due to openCount = openCount + sumRangeValues[row][col];
According to your example sumRangeValues[row][col] isn't an int. int + not an int = ??? if you want to keep a count of things you probably want openCount++ to replace that line instead, which is just a shortcut to openCount = openCount + 1;
Even if sumRangeValues[row][col] was an int, that line still wouldn't be what you're looking for. If you're searching for all the 3s in your spreadsheet your code would find your first 3, and then that line would execute 0 = 0 + 3 congrats, you just found 3 threes. You would continue to add three every time you found a three.
Waqar's code is essentially your code (amazingly simplified, but it iterates the same way) except he doesn't check color and he uses ++ instead of that line.

Resources