Mr Shane
Mr Shane

Reputation: 636

How to append unappended rows of data from a source sheet to a target sheet, then mark as APPENDED to prevent duplicate appending

I am trying to write an Apps Script that when run (either from a trigger or a custom menu) it will:

My current issues are:

NEWDATA (source data) NEWDATA

ALLDATA (target data) ALLDATA

So, i need to know how to stop the script from running by the number of rows that are not appended, and to not include the "APPENDED" column (F), and to only mark each row after they have been appended.

Here is the current script:

function appendToRecords() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
/** set the name of the SOURCE_SHEET sheet */
  var sourceSheet = ss.getSheetByName("NEWDATA");
  var startRow = 2; // First row of data to process
  var numRows = sourceSheet.getLastRow()-1; // Number of rows to process
  var dataRange = sourceSheet.getRange(startRow, 1, numRows, sourceSheet.getLastColumn()); // Fetch the range of cells being used
  var sourceData = dataRange.getValues(); // Fetch values for each row in the Range.
  var APPENDED = 'APPENDED';
  for (var i = 0; i < sourceData.length; ++i) {
    var row = sourceData[i];
    if (row[5] != APPENDED) { // To prevent sending duplicates, check that not already APPENDED
/** set the name of the TARGET_SHEET */
  var targetSheet = ss.getSheetByName("ALLDATA")
/** Append NEWDATA to ALLDATA */
  var lastRow = targetSheet.getLastRow();
  targetSheet.getRange(lastRow + 1, 1, 10, 6)
              .setValues(sourceData);
  sourceSheet.getRange(startRow+i,6).setValue("APPENDED"); // Add APPENDED indication to end of row
    }}}

And here is link to file:

https://docs.google.com/spreadsheets/d/1gOgvvZlCQgZi837sapZ3S8M8m6I98VFnGQwPiTCSY-I/edit?usp=sharing

Any help will be appreciated, thank you.

Upvotes: 1

Views: 88

Answers (1)

pgSystemTester
pgSystemTester

Reputation: 9917

Looking through your code, the biggest issue I see is when you're setting values on target sheet, you're doing sourcedata instead of row. I cleaned up your code by isolating some constants and making a little more dynamic.. Ultimately you should try to use an array to set values at end of procedure, rather than using line by line updates, but for the sake of answering your question, try this:

/** @OnlyCurrentDoc*/
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sourceSheet = ss.getSheetByName("NEWDATA");
const targetSheet = ss.getSheetByName("ALLDATA");
const startRow = 2; // First row of data to process
const APPENDED = 'APPENDED';

function appendToRecords() {
  const numRows = sourceSheet.getLastRow() - 1; // Number of rows to process
  const dataRange = sourceSheet.getRange(startRow, 1, numRows, sourceSheet.getLastColumn()); // Fetch the range of cells being used
  const sourceData = dataRange.getValues(); // Fetch values for each row in the Range.
  const lastColumn = sourceData[0].length;

  for (var i = 0; i < sourceData.length; ++i) {
    var row = sourceData[i];
    if (row[lastColumn-1] != APPENDED) {
      ///you should not do this line by line, but all at oncw with an array
      row[lastColumn-1] = APPENDED;
      var lastRow = targetSheet.getLastRow();
      targetSheet.getRange(lastRow + 1, 1, 1, row.length).setValues([row]);
      sourceSheet.getRange(startRow + i, lastColumn).setValue("APPENDED");
    }
  }
}

Upvotes: 2

Related Questions