Monday, July 3, 2017

Google Sheet Script for border style


I have columns and rows: D69:J79 filled with data, data in rows is changing on daily basis, one day it’s D69:J79, another it’s shorter D69:J77 or D69:J75 etc, rows below are blanks accordingly, columns stay the same.

I want script to make regular square line border around this data, but with one rule - bottom line of square should be right below latest row with data. And left and right borders lenght should be fitted to rows with data only as well.

I am total noob in scripts, could somebody help me with proper script for this?
And i hope this script can be set to autorun, so i don’t have to run it each time.

Or maybe there is some other solution more simple :)


Try the following script code:


function fillBorders() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName("Sheet1");
  var r = s.getRange("D69:J79");
  var v = r.getValues();
  var firstRow = r.getRow();
  var lastRow = r.getLastRow();
  var lastFilledRow = lastRow;
  for(var i=v.length-1;i>=0;i--) {
    var isRowBlank = true;
    for(var j=0;j<v[0].length;j++) {
      if(v[i][j]!="") {
        isRowBlank = false;
        lastFilledRow = firstRow + i;


in the above script code, change the sheet name "Sheet1" as per your need.

Saturday, July 1, 2017

Google Sheet Script to extract URL from a Cell containing function HYPERLINK

In Google Sheet, if you have a cell or a range containing functions HYPERLINK and if you want to extract URL(s) from it then you can do it with the help of following script:


function extractURL() {
  var activeRange = SpreadsheetApp.getActiveRange();
  var activeSheet = activeRange.getSheet();
  var formula = activeRange.getFormula();
  if(formula=="") return "";
  var rangeA1Notation = formula.match(/\((.*)\)/).pop();
  var range = activeSheet.getRange(rangeA1Notation);
  var f = range.getFormulas();
  var v = range.getValues();
  for(var i=0;i<f.length;i++) {
    for(var j=0;j<f[0].length;j++) {
      if(f[i][j]=="") v[i][j]="";
      var matchURL = f[i][j].match(/\"(.*?)\"/);
      if(matchURL==null) v[i][j]="";
      else v[i][j]=matchURL.pop();
  return v;


copy the above script code in the script editor of your spreadsheet and then use custom function "=extractURL(A2:A10)" to extract the URLs from the range "A2:A10"


Monday, February 20, 2017

Google Script to auto delete old files from a Google Drive Folder

If you are creating auto backup of any file in Google Drive using script ( either daily or on any edits ), so the number of back up file keeps on increasing. Now, if you want to delete the excessive files, for example if you want to delete all the files except the last 100 back up files, then you can achieve it by using the following script code:


function deleteOldSheets() {
  var df = DriveApp.getFolderById("xxx_GOOGLE_DRIVE_FOLDER_ID_xxx");
  var arr = new Array();
  var files = df.getFiles();
  while( files.hasNext() ) {
    var f =;
    arr.push( [ [ f.getId() ] , [ f.getLastUpdated() ] ] );
  arr.sort( sortFunction );
  for( var i=100 ; i<arr.length ; i++ )
    DriveApp.getFileById( arr[i][0] ).setTrashed( true );

function sortFunction( a , b ) {
  var aDate = new Date(a[1]);
  var bDate = new Date(b[1]);
  if ( aDate === bDate ) return 0;
  else if ( aDate < bDate ) return 1;
  else return -1;


The above script code will help you to delete the back up files ( from a Google Drive folder ) which are excessive and not required anymore, the script will move these files to "Trash".

And if you want to automatically run the above script daily, then in the script editor you can set the trigger for it.

NOTE: In the above script you'll need to replace the Google Drive folder Id.