Search code examples
javascriptgoogle-apps-scriptgoogle-sheetsimportrange

Archive data row to another tab while data is imported through importrange (Google Sheets)


My script archives a row from "2019-2020" to "Fully Invoiced" tab. But, the row doesn't disappear from the "2019-2020" tab. I think this is because the data is imported from another spreadsheet through a importrange function. I would like this row to disappear.

function onEdit(event) {
  // target sheet of move to named Paid
  // getColumn with check-boxes is currently set to column 2 or B
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = event.source.getActiveSheet();
  var r = event.source.getActiveRange();

  // Message box when archiving a project
  var editedCell = event.range.getSheet().getActiveCell();
    if((s.getName() == "2019-2020")&& r.getColumn() == 2 && r.getValue() == true) {   
    var response = Browser.msgBox('Make sure the project has been fully invoiced before archiving it. Are you sure to continue?', Browser.Buttons.YES_NO);
      Logger.log(response);
  if (response == "yes") {
    Logger.log('The user clicked "Yes."');
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("Fully Invoiced");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);
  } else {
    Logger.log('The user clicked "No" or the dialog\'s close button.');
  }
  }

  // Unarchive a project from Fully Invoiced tab to 2019-2020 tab
  else if(s.getName() == "Fully Invoiced" && r.getColumn() == 2 && r.getValue() == false) {
    var row = r.getRow();
    var numColumns = s.getLastColumn();
    var targetSheet = ss.getSheetByName("2019-2020");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).moveTo(target);
    s.deleteRow(row);   
}
} 

Does anyone have an idea?


Solution

  • Then you need to delete the entry from the source file. IMPORTRANGE() will always pull in the range you request in view only mode, i.e. you can not change values. If you want to change / remove values, then you need to do that in the source file.

    • Source File = The file which contains the original data
    • Working File = The file which uses IMPORTRANGE to get data from the Source File