Thursday, 9 March 2023

Google Sheet + Apps Script : Day03 (Removes blank Cells in a column consisting of hyperlinked strings)

 

Turns a spaced column into a compressed one :

many blank cells in between ==>blank cols removed

function removeEmptyCellsInColumn() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];

    var start=130;
    var last=300;

    var i=start;
    var j=i+1;

    var rangeCut,rangePaste;
    var a_i;
    var a_j =sheet.getRange(j,2).getValue()

    while(j<last){
        if(a_j !== "") {// if a[j] isNOTempty //a[i+1] =a[j];
          rangePaste = sheet.getRange(i+1,2); //pulls up next val in  next contiguous empty col
          rangeCut =sheet.getRange(j,2);
          a_i =sheet.getRange(i,2).getValue();
          
          var richValue = SpreadsheetApp.newRichTextValue()
                        .setText(a_j)
                        .setLinkUrl(sheet.getRange(j,2).getRichTextValue().getLinkUrl())
                        .build();
          rangePaste.setRichTextValue(richValue); //paste at a[i]
          i++;
          rangeCut.clear();//clears or cuts the a[j]
        }
        j++; 
        a_j =sheet.getRange(j,2).getValue();
      }
}

No comments:

Post a Comment