Monday, August 12, 2013

Google Spreadsheet Script to widen cell using button

Question:

( by Jonny Litten )


Hi all,
I have no knowledge of javascript, but I would like to make a script that will widen/lengthen the active cell when I click a button. If it could also shrink upon clicking the button a second time that would be ideal. Does anyone have any ideas as to how to accomplish this?
Thank you so much,
Jonny

Solution:

To have a button, you can insert an image which looks like a button.
To insert image, go to: "Insert" > "Image".
And then you can click on the on the small arrow on that image which will show you option of "Assign script..." and then put the custom function "widenActiveCell" and then click "OK".
And then you can click on this so called button to set the width and length of active cell.

Have a look at the following animated screenshot:



Have a look at the following code:

///////////////////////////////////////

function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [ {name:"Widen Cell",functionName:"widenActiveCell"} ];
  sheet.addMenu("Script", entries);
};

function widenActiveCell() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var cell = sheet.getActiveCell();
  if(sheet.getColumnWidth(cell.getColumn()) == 100) {
    sheet.setColumnWidth(cell.getColumn(),10);
    sheet.setRowHeight(cell.getRow(),10);
  }
  else {    
    sheet.setColumnWidth(cell.getColumn(),100);
    sheet.setRowHeight(cell.getRow(),100);
  }
};

///////////////////////////////////////

put the above code in your script editor, and then you can use it directly in your Google Spreadsheet.


Click on the button "Click Here", then script will check whether the cell is already widen, if yes then it will shrink it or else it will widen it.


And If you are not much familiar with scripts then check out the following link:
http://igoogledrive.blogspot.com/2012/08/how-to-write-script-in-google.html 

I hope the above solution will help you, and if you need more help then please do comment below on this blog itself, I will try to help you out.


I also take up private and confidential projects:
If this blog post was helpful to you, and if you think you want to help me too and make my this blog survive then please donate here: http://igoogledrive.blogspot.com/2012/09/donate.html 

Thanks,

No comments:

Post a Comment