0
votes

This function works fine. When the current sheet is not a Google form response sheet. I am using (Everyminute) Trigger

Image of: Sand data (mainData) sheet to (shareData1) sheet

  • I am looking for a, When the user submits the Google form whose status is ( Paid ), the data should go on the second sheet which is (shareData1)

The problem here is that this function will not work anymore when I am using the Google Forms response sheet. And it shows an error. Error Image

in the error image for demotion, I am run this function manually, the same error show in Executions AppScript panel.

function doneCopy() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("mainData");
  var values = sheet.getRange(1, 3, sheet.getLastRow(), 1).getValues();

  var moveRows = values.reduce(function(ar, e, i) {
    if (e[0] == "Paid" ) ar.push(i + 1)
    return ar;

  }, []);

  var targetSheet = ss.getSheetByName("shareData1");

  moveRows.forEach(function(e) {
    sheet.getRange(e, 1, 1, sheet.getLastColumn()).moveTo(targetSheet.getRange(targetSheet.getLastRow() + 1, 1));
  });
  moveRows.reverse().forEach(function(e) {sheet.deleteRow(e)});
}

1

1 Answers

1
votes
function doneCopy() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sh = ss.getSheetByName("mainData");
  const tsh = ss.getSheetByName("shareData1");
  const vs = sh.getRange(1, 3, sh.getLastRow(), 1).getValues().flat();
  const n = sh.getLastColumn();
  let d = 0;
  vs.forEach((e, i) => {
    if (e == 'Paid') {
      sh.getRange(i + 1, 1, 1, n).copyTo(tsh.getRange(tsh.getLastRow() + 1, 1));
      sh.deleteRow(i + 1 - d++);
    }
  });
}

I'm not sure what your additional needs are but I'll take my best guess.

function doneCopy() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sh = ss.getSheetByName("mainData");
  const tsh = ss.getSheetByName("shareData1");
  const vs = sh.getRange(1, 3, sh.getLastRow(), 1).getValues().flat();
  const n = sh.getLastColumn();
  const a = ['Paid','Process'];
  let d = 0;
  vs.forEach((e, i) => {
    if (~a.indexOf(e)) {
      sh.getRange(i + 1, 1, 1, n).copyTo(tsh.getRange(tsh.getLastRow() + 1, 1));
      sh.deleteRow(i + 1 - d++);
    }
  });
}