Created
February 24, 2020 21:22
-
-
Save AspenJames/f1857a899893cfdafe27bc705cccb497 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function makeRangeAbsoluteReference() { | |
// This grabs the currently selected cells, | |
// applying _makeAbsolute() to each | |
var spreadsheet = SpreadsheetApp.getActive(); | |
var range = spreadsheet.getActiveRange(); | |
var numRows = range.getNumRows(); | |
var numCols = range.getNumColumns(); | |
for (var i = 1; i <= numRows; i++) { | |
for (var j = 1; j <= numCols; j++) { | |
var currentCell = range.getCell(i,j); | |
_makeAbsolute(currentCell); | |
} | |
} | |
}; | |
function _makeAbsolute(cell) { | |
// This function accepts a reference to a cell which in turn | |
// references another cell relatively. The cell reference is updated | |
// to an absolute reference, ex. "C4" => "$C$4". | |
// Currently only works for direct cell references with no manipulation | |
// (won't work for calculated values such as "C4 + F2") | |
var formula = cell.getFormula(); | |
var formArr = formula.match(/([A-Z]+)(\d+)/); | |
formArr.shift() // get rid of entire match, pieces remain | |
var absForm = formArr.map(n => `$${n}`).join(''); | |
cell.clearContent(); | |
cell.setFormula(absForm); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment