Home > Blockchain >  Not able to copy sheet1 data to sheet2 in specific format
Not able to copy sheet1 data to sheet2 in specific format

Time:11-17

https://docs.google.com/spreadsheets/d/18odtLaamxJISS7Gq-N-xPxVu1Fk55AYQoFojkmIgxbM/edit#gid=0

function myFunction() {
  
  function sendEmails() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Messages from BOs");
  var startRow = 4; // First row of data to process
  var numRows = 1; // Number of rows to process
  // Fetch the range of cells A4:C3
  var dataRange = sheet.getRange(startRow, 1, numRows, 3);
  // Fetch values for each row in the Range.
  var data = dataRange.getValues();
  
  for (var i in data) {
    var row = data[i];
    var emailAddress = row[0]; // First column
    var subject = row[1];
    var message = row[2]; // Second column
    MailApp.sendEmail(emailAddress, subject, message);
  }

  

  for (var i in data, i  ) {
    var row1 = data[i];
    Browser.msgBox(data[i 1])
    /**
    var emailAddress1 = row[0]; // First column
    var subject2 = row[1];
    var message3 = row[2]; // Second column
    */
    var ad = row1;
    var ts = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Test');
    ts.getRange(startRow, i 1 ).setValue(row1);
    //i = i   1;
    }

}
}

Here i wanted to update sheet2 every time when email send to "Recipient EMAL ID" to the new row of sheet2. Please see my google sheet. I tried below code but it does not work well. If any who can help me in solve this problem.

CodePudding user response:

Try to change:

ts.getRange(startRow, i 1 ).setValue(row1);

with:

ts.appenRow(row1);

https://developers.google.com/apps-script/reference/spreadsheet/sheet#appendRow(Object)

CodePudding user response:

This is a lot simpler:

function sendEmails() {
  const ss = SpreadsheetApp.getActive()
  var sh = ss.getSheetByName("Messages from BOs");
  var ts = ss.getSheetByName('Test');
  var sr = 4; // First row of data to process 
  var rg = sh.getRange(sr, 1, 1, 3);
  var vs = rg.getValues();
  vs.forEach(row => {
    MailApp.sendEmail(row[0], row[1], row[2]);
    ts.appendRow(row);
    });
}
  • Related