简体   繁体   中英

Search string in google sheet using google apps script

I have 2 columns which are user name and user telephone number in google sheet. I have a variable to store the input user name and I want to find the phone number of that user name whenever I change the input user name. Very appreciate your help.

  function Showtel(){
  var app = SpreadsheetApp;
  var ss = app.getActiveSpreadsheet();
  var ActiveSheet = ss.getActiveSheet();
  var searchSheet = ss.getSheetByName('Sheet1');

You can use a trigger called onEdit that runs a function every time the user edits the sheet.

Here is the sample code provided by the docs

function onEdit(e){
   // Set a comment on the edited cell to indicate when it was changed.
   var range = e.range;
   range.setNote('Last modified: ' + new Date());
}

The e parameter is an Event Object which contains some data about the edit done by the user, which includes the range (which is the user name cell that changed).

You can use this information to reference the range that contains the telephone number via the offset method . Then you can get the value of the cell via getValue method.

In short, assuming the phone number is one column to the right of the name, the code would look similar to this:

function onEdit(e){
   // Get value of the cell one column to the right of edited cell
   var usernameRange = e.range;
   var phoneRange = range.offset(0, 1);
   var phoneNumber = phoneRange.getValue()
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM