Advertisement
BaneD

Google SpreadSheet - extract hyperlink into string

Apr 29th, 2015
5,383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   taken from https://productforums.google.com/forum/#!topic/docs/ymxKs_QVEbs
  3.   created by --Hyde
  4. */
  5. /** Extract a text string in double quotes from the formulas in selected cells
  6. */
  7.  
  8.  
  9. function replaceFormulasWithFirstQuotedTextStringInFormula() {
  10.   // Goes through all the cells in the active range (i.e., selected cells),
  11.   // checks if a cell contains a formula, and if so, extracts the first
  12.   // text  string in double quotes in the formula and stores it in the cell.
  13.   // The formula in the cell is replaced with the text string.
  14.   // see https://productforums.google.com/d/topic/docs/ymxKs_QVEbs/discussion
  15.  
  16.   // These regular expressions match the __"__ prefix and the
  17.   // __"__ suffix. The search is case-insensitive ("i").
  18.   // The backslash has to be doubled so it reaches RegExp correctly.
  19.   // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/RegExp
  20.  
  21.   var prefix = '\\"';
  22.   var suffix = '\\"';
  23.   var prefixToSearchFor = new RegExp(prefix, "i");
  24.   var suffixToSearchFor = new RegExp(suffix, "i");
  25.   var prefixLength = 1; // counting just the double quote character (")
  26.  
  27.   var ss = SpreadsheetApp.getActiveSpreadsheet();
  28.   var activeRange = ss.getActiveRange();
  29.   var cell, cellValue, cellFormula, prefixFoundAt, suffixFoundAt, extractedTextString;
  30.  
  31.   // iterate through all cells in the active range
  32.   for (var cellRow = 1; cellRow <= activeRange.getHeight(); cellRow++) {
  33.     for (var cellColumn = 1; cellColumn <= activeRange.getWidth(); cellColumn++) {
  34.       cell = activeRange.getCell(cellRow, cellColumn);
  35.       cellFormula = cell.getFormula();
  36.      
  37.       // only proceed if the cell contains a formula
  38.       // if the leftmost character is "=", it contains a formula
  39.       // otherwise, the cell contains a constant and is ignored
  40.       // does not work correctly with cells that start with '=
  41.       if (cellFormula[0] == "=") {
  42.        
  43.         // find the prefix
  44.         prefixFoundAt = cellFormula.search(prefixToSearchFor);
  45.         if (prefixFoundAt >= 0) { // yes, this cell contains the prefix
  46.           // remove everything up to and including the prefix
  47.           extractedTextString = cellFormula.slice(prefixFoundAt + prefixLength);
  48.           // find the suffix
  49.           suffixFoundAt = extractedTextString.search(suffixToSearchFor);
  50.           if (suffixFoundAt >= 0) { // yes, this cell contains the suffix
  51.             // remove all text from and including the suffix
  52.             extractedTextString = extractedTextString.slice(0, suffixFoundAt).trim();
  53.            
  54.             // store the plain hyperlink string in the cell, replacing the formula
  55.             cell.setValue(extractedTextString);
  56.           }
  57.         }
  58.       }
  59.     }
  60.   }
  61. }
  62.  
  63.  
  64. /** Add a custom menu to the active spreadsheet, containing a single menu item
  65. *   for invoking the replaceFormulasWithFirstQuotedTextStringInFormula() function.
  66. *   The onOpen() function is automatically run when the spreadsheet is opened.
  67. */
  68.  
  69.  
  70. function onOpen() {
  71.   var ss = SpreadsheetApp.getActiveSpreadsheet();
  72.   var entries = [{
  73.     name : "Replace formulas with text strings",
  74.     functionName : "replaceFormulasWithFirstQuotedTextStringInFormula"
  75.   }];
  76.   ss.addMenu("Extract", entries);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement