0
votes

I’m trying to get the value of column Notes in Sheet 2 and put it to column Notes of Sheet 1. To do that, they should have the same column Company value. When I enter a value in Company (Sheet 2), the code will find if it has the same value in Company (Sheet 1). If the code finds the same value of Company, the value in Notes (Sheet 2) that I enter will automatically put in Notes (Sheet 1). I used the if/else statement in my code but it is not running.

Any help would be greatly appreciated.

Ex:

Sheet 1 enter image description here

Sheet 2 enter image description here

Here's my code :)

function onEdit() {
  var sheetId = SpreadsheetApp.openById('1fx59GrC_8WEU5DCM5y2N8YR3PK936RLqZVknQ66WWUA'); //replace with source ID
  var sourceSheet = sheetId.getSheetByName('Notes'); //replace with source Sheet tab name
  var notesActiveCell = sourceSheet.getActiveCell();
  var notesRow = notesActiveCell.getRow();
  var notesComp = SpreadsheetApp.getActiveSheet().getRange('B' + notesRow.toString()); 

  
  var destSheet = sheetId.getSheetByName('Companies'); //replace with destination Sheet tab name
  var compActiveCell = destSheet.getActiveCell();
  var compRow = compActiveCell.getRow();
  var companies = SpreadsheetApp.getActiveSheet().getRange('B' + compRow.toString());
  
  
  if (notesComp.getValues() == companies.getValues()) {
    Logger.log(notesComp.getValues());
    var sourceRange = SpreadsheetApp.getActiveSheet().getRange('C' + notesRow.toString()); //assign the range you want to copy
    var notesData = sourceRange.getValues();
    destSheet.getRange('F' + compRow.toString()).setValues(notesData);
  }
}
2

2 Answers

0
votes

There are a lot of errors in your code. I'm not a very experienced programmer, but form what you have said I think the script below should give you a good starting point.

Don't forget, you also need to add an onEdit trigger to run the script when you enter a value.

function onEdit(e) {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getActiveSheet(); 
var sourceCellRow = sourceSheet.getActiveCell().getRowIndex();
var sourceData = sourceSheet.getRange(sourceCellRow, 1, 1, 2).getValues();

var destSS = SpreadsheetApp.openById('Paste destination Spreadsheet ID here');
var destSheet = destSS.getSheetByName('Companies');
var searchData = destSheet.getRange(1, 1,     destSheet.getLastRow()).getValues();

for (var i = 0; i < searchData.length; i ++){
   if(searchData[i][0] == sourceData[0][0]){
      destSheet.getRange(i + 1, 2).setValue(sourceData[0][1]);
      break;
     }     
    }
   }
0
votes

Try using the copyTo(destination)

Copies the data from a range of cells to another range of cells. Both the values and formatting are copied.

// The code below will copy the first 5 columns over to the 6th column.
var sheet = SpreadsheetApp.getActiveSheet();
var rangeToCopy = sheet.getRange(1, 1, sheet.getMaxRows(), 5);
rangeToCopy.copyTo(sheet.getRange(1, 6));
}

I think that will be invaluable inside your onEdit() function.\

You might also want to check this sample from this google forum

function Copy() {
var sss = SpreadsheetApp.openById('spreadsheet_key'); //replace with source ID
var ss = sss.getSheetByName('Source'); //replace with source Sheet tab name
var range = ss.getRange('A2:E6'); //assign the range you want to copy
var data = range.getValues();

var tss = SpreadsheetApp.openById('spreadsheet_key'); //replace with destination ID
var ts = tss.getSheetByName('SavedData'); //replace with destination Sheet tab name
ts.getRange(ts.getLastRow()+1, 1,5,5).setValues(data); //you will need to define the size of the copied data see getRange()
}