Skip to content

Instantly share code, notes, and snippets.

@documents-design
Last active April 29, 2019 08:42
Show Gist options
  • Select an option

  • Save documents-design/5c661eefbc0b045597965e738e46dc1b to your computer and use it in GitHub Desktop.

Select an option

Save documents-design/5c661eefbc0b045597965e738e46dc1b to your computer and use it in GitHub Desktop.
Copy range formatting across google sheets
/*
https://webapps.stackexchange.com/questions/50409/cell-reference-with-colour-formatting/50623#50623
https://stackoverflow.com/questions/21229180/convert-column-index-into-corresponding-column-letter
*/
function cellString(tl, br) {
return br ? tl + ':' + br : tl;
}
function getCell(sheet, celltl, cellbr) {
var s = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet);
if (!s) return null;
var c = s.getRange(cellString(celltl, cellbr));
if (!c) return null;
return c;
}
function getCellRange(sheet, row, col, numrows, numcols) {
var s = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheet);
if (!s) return null;
Logger.log({row: row, col: col, numrows: numrows, numcols: numcols});
var c = s.getRange(row, col, numrows, numcols);
if (!c) return null;
return c;
}
function browseCells(callback) {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getDataRange();
var offsetRow = range.getRow() - 1;
var offsetCol = range.getColumn() - 1;
var cells = range.getFormulas();
for (var row = 0; row < cells.length; row ++ ) {
for (var column = 0; column < cells[row].length; column ++ ) {
callback(sheet, cells, row, column);
}
}
}
function cloneCcCells() {
browseCells(function(sheet, cells, row, column) {
var ref = findReferenceCells(cells[row][column]);
if (ref){
o = getCell(ref.sheet, ref.celltl, ref.cellbr);
var r = colToInteger(ref.celltl[0]);
var c = ref.celltl.slice(1);
var h = parseInt(ref.cellbr.slice(1), 10) - parseInt(ref.celltl.slice(1), 10) + 1;
var w = colToInteger(ref.cellbr[0]) - colToInteger(ref.celltl[0]) + 1;
n = getCellRange(sheet.getName(), row + 1, column + 1, h, w);
n.setBackgrounds(o.getBackgrounds());
n.setFontColors(o.getFontColors());
n.setFontFamilies(o.getFontFamilies());
n.setFontWeights(o.getFontWeights());
n.setFontStyles(o.getFontStyles());
n.setFontSizes(o.getFontSizes());
n.setVerticalAlignments(o.getVerticalAlignments());
n.setHorizontalAlignments(o.getHorizontalAlignments());
n.setNumberFormats(o.getNumberFormats());
}
});
}
function findReferenceCells(cellValue) {
if (cellValue === "") {
return false;
}
var refPattern = /^=importrange\("[^"]+"\s*;\s*"([^"]+)!([^:"]+):?([^"]+)?"\)$/i;
var matches = refPattern.exec(cellValue.replace(" ", ""));
if (!matches) {
return false;
}
var sheet = matches[1].replace("\"", "");
var celltl = matches[2].replace("\"", "");
var cellbr = matches.length > 3 ? matches[3] : null;
return { sheet: sheet, celltl: celltl, cellbr: cellbr };
}
function colToInteger(columnName){
var nameParts = columnName.toLowerCase().split();
//97 is char code of "a", but we need 1 based indices
var colNum = nameParts.pop().charCodeAt(0) - 96;
if (nameParts.length === 1){
colNum += 26 * (nameParts.pop().charCodeAt(0) - 96);
}
return colNum;
}
function columnToLetter(column)
{
var temp, letter = '';
while (column > 0)
{
temp = (column - 1) % 26;
letter = String.fromCharCode(temp + 65) + letter;
column = (column - temp - 1) / 26;
}
return letter;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment