Skip to content

Instantly share code, notes, and snippets.

@homburg
Created June 18, 2013 18:04
Show Gist options
  • Save homburg/5807786 to your computer and use it in GitHub Desktop.
Save homburg/5807786 to your computer and use it in GitHub Desktop.
Google spreadsheets scripting: search/replace hyperlinks
function MogensHyperlinkOffset() {
var sheet = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ranges = ["M:M", "Q:Q"];
var totalCells = 0;
for (var r in ranges) {
ss.toast("Running range: "+ranges[r])
var dRange = sheet.getRange(ranges[r])
var numRows = dRange.getNumRows()
var numCols = dRange.getNumColumns()
ss.toast(numRows + " rows")
Logger.log(numRows + " rows and "+numCols+" columns")
for (var i = 1; i <= numRows; i++) {
for (var j = 1; j <= numCols; j++) {
cell = dRange.getCell(i, j)
form = cell.getFormula()
if (form != "") {
mIndex = form.indexOf("Mogens")
Logger.log({form: form, mIndex: mIndex});
if (i % 20 == 0) {
ss.toast("row: "+i);
}
if (mIndex > 20 && form.indexOf("HYPERLINK") > 0) {
totalCells++;
cell.setFormula('=HYPERLINK("file:///'+form.substr(mIndex))
}
}
}
}
ss.toast("Range done: "+ranges[r])
}
ss.toast(totalCells, "Total number of cells edited", 300)
};
function hello() {
Browser.msgBox("Hellooo!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment