Home > Mobile >  How can I conditionally copy between Google sheets?
How can I conditionally copy between Google sheets?

Time:09-17

I would like to conditional copy a value from one sheet to another.

About the below script:

  • It checks if in SOURCE tab the value in column H is equal to 45 and if Column A is empty. If so, it should copy the value in Column G into the last row of column A in TARGET tab. It also ads data to some of the TARGET tab columns

  • If I change this: sourceSheet.getRange(rangeToCopy).copyTo(destination.getRange(destination.getLastRow() 1, 1)),{contentsOnly:true}; to this: sourceSheet.getRange(rangeToCopy).copyTo(destination.getRange(destination.getLastRow()-1, 1)),{contentsOnly:true}; the cell is pasted on Column A on the penultimate row, which makes me think that the present code tries to copy to a row after the last visible one instead of pasting into the next blank cell after a non blank one in Column A, hence returning the error explained on the next bullet

  • As is, it returns the error: The coordinates of the range are outside the dimensions of the sheet. (I understood that the problem is because of the use of getlasrow and being used as is, it's trying to retrieve a row that does not exist)

This is the code so far:

 function CopyTo(){
  var sheet = SpreadsheetApp.getActive();
  var sourceSheet = sheet.getSheetByName("SOURCE");
  var destination = sheet.getSheetByName("TARGET");
  var lastRow = sourceSheet.getDataRange().getLastRow();

  for(var row=3; row<=lastRow; row  ){
  if(sourceSheet.getRange(row,1).getValue() == "" & sourceSheet.getRange(row,8).getValue() >= "45"){
 Logger.log("CELL H" row " has \">=45\"" "\n=================\n" "CELL A" row " is \"empty\"" "\n=================\n" "RANGE TO COPY:\n"  "\"G" row ":G" row "\"");
      var rangeToCopy = "G" row ":G" row;
      sourceSheet.getRange(rangeToCopy).copyTo(destination.getRange(destination.getLastRow() 1, 1)),{contentsOnly:true};
      destination.getRange(destination.getLastRow(), 9).setValue("Added Text Column 9");
      destination.getRange(destination.getLastRow(), 13).setValue("Added Text Column 13");
      destination.getRange(destination.getLastRow(), 14).setValue(new Date());
    }
  }
}

Also, there are other columns in Target sheet that fetch values so maybe that's the reason why the script isn't pasting on the right place but I can't get it to work.

Could you be so kind to help out? Thank you in advance.

This is enter image description here

To solve this, create a new TARGET sheet or find a way to remove the limit in the current TARGET sheet. Your current code should be fine.

EDIT:

I've edited your v2 code to make it work on the current test sheet provided. Please see code below.

   function CopyTo_V2(){
  var sheet = SpreadsheetApp.getActive();
  var sourceSheet = sheet.getSheetByName("SOURCE");
  var destination = sheet.getSheetByName("TARGET");
  var lastRow = sourceSheet.getDataRange().getLastRow();
  var column = destination.getRange('A'   destination.getMaxRows())

  for(var row=3; row<=lastRow; row  ){
  if(sourceSheet.getRange(row,1).getValue() == "" & sourceSheet.getRange(row,8).getValue() >= "45"){
 Logger.log("CELL H" row " has \">=45\"" "\n=================\n" "CELL A" row " is \"empty\"" "\n=================\n" "RANGE TO COPY:\n"  "\"G" row ":G" row "\"");
      var rangeToCopy = "G" row ":G" row;
      var lastFilledRow = parseInt(column.getNextDataCell(SpreadsheetApp.Direction.UP).getA1Notation().slice(1))
      Logger.log(lastFilledRow);
      sourceSheet.getRange(rangeToCopy).copyTo(destination.getRange(lastFilledRow 1,1)),{contentsOnly:true};
      destination.getRange(lastFilledRow 1, 9).setValue("Added Text Column 9");
      destination.getRange(lastFilledRow 1, 13).setValue("Added Text Column 13");
      destination.getRange(lastFilledRow 1, 14).setValue(new Date());
    }
  }
}
  • Related