Tuesday, September 4, 2012

How to get the value of the active cell in another cell

How to get the value of the active cell in another cell.


Question:

(by Darnell)
Is there a way through simple scripting to continuously update the value of a specific cell on a sheet with the value of where the cursor is currently placed (that is "active cell")?

I want to automatically choose a name in a list of names (here in below example "Column A" and "Column B") as someone highlights that cell, and get that name which will be used in a query function which is built of the value of the cell in L2.


Solution:


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


Here is the script for achieving the above mentioned task:

//===================================================


function onOpen() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{name : "Get Active Cell",functionName : "GetActiveCell"}];
  sheet.addMenu("Scripts", entries);
};


function GetActiveCell() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var range = sheet.getDataRange();
  var activecell = sheet.getActiveCell();
  var valueactivecell =activecell.getValue();
  range.offset(1, 11, 1, 1).setValue(valueactivecell); // writes the value of the active cell in L2
}

//===================================================



Now save the above script in the script editor, and run function "GetActiveCell", you will get the value from the cell which is currently active on the spreadsheet, and it will update the "Cell L2"

Have a look at the screenshot below:




Now, to run this script automatically, you can set triggers. But the sad thing is Trigger can be set to every minute so you cant get the value of selected cell in every second or so. But you can run this script as shown in above screenshot.

Now, How to set triggers? 
In the script editor, you have to click on the icon of "Trigger" and then set the events at which you want to execute these triggers.
Have a look at the following screenshot:



If you are new to triggers, and want to know step by step then have a look at the following link:
http://igoogledrive.blogspot.in/2012/08/how-to-set-trigger-on-script-in-google.html



If you are new to Google Spreadsheet Scripts, then please have a look at the following link:


I hope this script will help you, and if you need more help then please do comment below on this blog itself.

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

Thanks,
Kishan,


3 comments:

  1. It's very good and how to get values of the row with active cell in another row?

    ReplyDelete
  2. How to get value don't use addmenu

    only click mouse active cell then get value

    Thanks

    ReplyDelete
  3. How do I get the value 2 columns to the left or right of the active cell?

    ReplyDelete