Home > Back-end >  Google Script copy paste
Google Script copy paste

Time:11-09

Right now it overwrite same cell in Logs I need it to write to next cell and not overwrite the same cell. https://freeimage.host/I/5GIOjs I want to Search for D3=Lumee in the Sheet called Logs in colum A u see all the names. Want it co copy J12 to the specific Name https://freeimage.host/i/5GTLJI

function myFunction() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getActiveSheet();
  var j12 = s.getRange('J12').getValues();

  var dynamic_cell = s.getRange('D3').getValue();

  var target_s = ss.getSheetByName('Logs');

  for (var i = 2; i<=target_s.getMaxRows();i  ){
    var match = target_s.getRange(i, 1).getValue();
    if(dynamic_cell == match){
      var targetrange = target_s.getRange(i,2,1);
      break;
    }
  }

  targetrange.setValues(j12);

}

CodePudding user response:

This is how I'd do it.

function myFunction() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const s = ss.getActiveSheet();
  const j12 = s.getRange('J12').getValue();
  const d3 = s.getRange('D3').getValue();
  const tsh = ss.getSheetByName('Logs');
  const vs = tsh.getRange(2,1,tsh.getLastRow() -1).getValues();
  for( let i = 0;i<vs.length; i  ) {
    if(d3 == vs[i][0]){
      tsh.getRange(i   2,1).setValue(j12);
      break;
    }
  }
}

It's a lot faster than pulling the match value out during each loop. This way I get all of them all at one time and the loop run's much faster.

CodePudding user response:

function myFunction() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getActiveSheet();
  var j12 = s.getRange('J12').getValues();

  var dynamic_cell = s.getRange('D3').getValue();

  var target_s = ss.getSheetByName('Logs');

  for (var i = 2; i<=target_s.getMaxRows();i  ){
    var match = target_s.getRange(i, 1).getValue();
    if(dynamic_cell == match){
      var targetrange = target_s.getRange(i,2,1);
      break;
    }
  }

  targetrange.setValues(j12);

}
  • Related