简体   繁体   中英

Google App Script update cell value in another sheet

I'm a little stuck on this one. I'm trying to find the corresponding row and update the last column in another google spreadsheet after updating the first column of another spreadsheet.

When the user selects "Restocked" in ColA of spreadsheet X , I need to lookup the ID value in ColB on another sheet (Y). Then I need to access spreadsheet Y, find the row that contains that same ID. Access the last column or columnAZ (52) and change the cell value to "Restocked".

Here is what I have so far...

function restockComplete(){

var s = SpreadsheetApp.getActiveSpreadsheet();
  if( s.getName() == "Restock Queue"){
var r = s.getActiveCell();
  if ( r.getColumn() == 1 && r.getValue() == "Restocked"){
  var nextCell = r.offset(0, 1);
  var buybackId = nextCell.getValue();

  // Opens SS by its ID
  var ss = SpreadsheetApp.openById("xxxxxxxxxxxxxxxSheetIDHerexxxxxxxxxxx"); 

  var sheet = ss.getSheetByName('NameOfSheetHere'); // Name of sheet
  // var range = sheet.getRange(1,1); // Gets Column 1 Cell 1 value
  //var data = range.getValue(); 

 var data = sheet.getDataRange().getValues();
  //var buyback = sheet.getRange(buybackId).getValue();
  for(var i = 0; i<data.length;i++){
    if(data[i][1] == buybackId){ //[1] because column B
      Logger.log((i+1))
      i.offset(0, 52).setValue('Restocked');
      return i+1;
     }
    }   
   }
  }
};

You're close, save for one error. Right now, to test for the sheet name, you have to actually get the sheet. I would do the following to fix the immediate problem:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getActiveSheet()  // call this for the active sheet

...

You also had an error in your loop setting the value:

for(var i = 0; i<data.length;i++){
  if(data[i][1] == buybackId){ //[1] because column B

    // Get the range of the cell, not the index.
    sheet.getRange((i+1), 1).setValue('Restocked');
  }
}
...

Rename your secondary sheet from ss to something else to avoid a conflict.

Beyond this fix, there are some changes I'd recommend making to make the script more efficient for your users. Rather than look for the active cell, you can use the onEdit simple trigger with an e event object. This will allow your script to modify cells regardless of where the active cell is.

function onEdit(e){

  var ss = SpreadsheetApp.getActiveSpreadsheet()
  var s = ss.getActiveSheet();
  if( s.getName() == "Restock Queue"){

      if ( (e.range.getColumn() == 1.0) && (e.range.getValue() == "Restocked") ){

        var nextCell = e.range.offset(0, 1);
        var buybackId = nextCell.getValue();

        var ss2 = SpreadsheetApp.openById('xxx');

        var sheet = ss2.getSheetByName('NameOfSheetHere'); // Name of sheet

        var data = sheet.getDataRange().getValues();
        for(var i = 0; i<data.length;i++){
          if(data[i][1] == buybackId){ //[1] because column B

            // Get the range of the cell, not the index.
            sheet.getRange((i+1), 52).setValue('Restocked');
          }
        }
      }
  }
}

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