0

I have 2 different sheets, the first is where all my data is stored and changes. On updating the first sheet it moves to the second sheet. I am trying to figure out if after the update, I can move a row from the second sheet to another tab in the second sheet based off of the data in column Q or col 17. Here is what I currently have, but it does not seem to be moving anything. Is this even possible?

function onUpdate() {

  var sheetNameToWatch = "Raw Data";
  var columnNumberToWatch = 17; // column A = 1, B = 2, etc.
  var valueToWatch = ["Full", "Partial", "Process", "Conditional", "Fail", "Exemption"];
  var sheetNameToMoveTheRowTo = (valueToWatch + " Certification");;

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getActiveCell();

  if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
    var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
    var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
    sheet.deleteRow(range.getRow());
  }
}

Thanks.

Ok, I've updated the current script:

function onUpdate() {
  var sourceSheet = "Raw Data";
  var columnNumberToWatch = 17; // column A = 1, B = 2, etc.
  var valueToWatch = ["Full", "Partial", "Process", "Conditional", "Fail", "Exemption"];
  var sheetNameToMoveTheRowTo = (valueToWatch + " Certification");

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getActiveCell();

  if (sheet.getName() === sourceSheet && range.getColumn() === columnNumberToWatch && checkMatch(range.getValue(), valueToWatch) === true) {
    var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
    var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
    sheet.deleteRow(range.getRow());
      }
}

function checkMatch(value, array) {
  for (i in array) {
    if (value === array[i]) {
      return(true);
    }
  }
}

Alright,

I tried to specify a little more with it:

function onEdit() {
  var sourceSheet = "Raw Data";
  var columnNumberToWatch = 17; // column A = 1, B = 2, etc.
  var valueToWatch = ["Full", "Partial", "Process", "Conditional", "Fail", "Exemption"];
  var sheetNameToMoveTheRowTo = (valueToWatch + " Certification");

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getActiveCell();
  var cert = range.getValue();

  if (valueToWatch.indexOf(cert) === sourceSheet && range.getColumn() === columnNumberToWatch && checkMatch(range.getValue(), valueToWatch) === true) {
    var sheetNameToMoveTheRowTo = (cert + " Certification");
    var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
    var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
    sheet.deleteRow(range.getRow());
      }
}

function checkMatch(value, array) {
  for (i in array) {
    if (value === array[i]) {
      return(true);
    }
  }
}

by defining cert and trying to define more clearly the array for valueToWatch.

baldalbino
  • 17
  • 1
  • 5

1 Answers1

0

You need to change your if statement. It should be "===" instead of "==". Here is a good explanation behind this.

Here is the final script that you can use

function onEdit(e) {
  var sourceSheet = "Raw Data";
  var columnNumberToWatch = 17; // column A = 1, B = 2, etc.
  var valueToWatch = ["Full", "Partial", "Process", "Conditional", "Fail", "Exemption"];
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var range = e.range;
  var activesheet = range.getSheet();
  var activesheetname = activesheet.getName();
  var row = range.getRow();
  var value = ss.getSheetByName(sourceSheet).getRange(row, columnNumberToWatch).getValue();

  if (activesheetname === sourceSheet && valueToWatch.indexOf(value) >=0) {
    var targetSheet = ss.getSheetByName(value + " Certification");
    var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    activesheet.getRange(row, 1, 1, activesheet.getLastColumn()).moveTo(targetRange);
    activesheet.deleteRow(row);
      }
}

Update You cannot make a script run on the second spreadsheet without manually editing a cell. Once the row gets pushed from first spreadsheet in two second, it will not trigger the onEdit function in the second spreadsheet. What you could do is set it to allocate the row to correct sheet from the first spreadsheet with something like this

 //This is part of a script on the first spreadsheet   
 if (valueToWatch.indexOf(value) >=0) {
        var targetSheetName = value + " Certification";
      } else {
        var targetSheetName = "Raw Data";
      }
        var targetSheet = SpreadsheetApp.openById("1bjLKWXUwM6UE4SHz2pbhH3F1E-8GKtvV0870YetOpKo").getSheetByName(targetSheetName);
        var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1,1,activesheet.getLastColumn());
        targetRange.setValues(activesheet.getRange(row, 1, 1, activesheet.getLastColumn()).getValues());
        activesheet.deleteRow(row);

    }
Community
  • 1
  • 1
Akshin Jalilov
  • 1,658
  • 1
  • 12
  • 12
  • Thanks for the reply. Do I need to define the array or value in the second? I wouldn't think so as the [ ] should identify the array. However, nothing is moving to the respective tabs. Any additional thoughts? – baldalbino May 14 '15 at 20:16
  • Have you set up an onEdit trigger? – Akshin Jalilov May 14 '15 at 20:18
  • You could call your function onEdit() instead of onUpdate(). – Akshin Jalilov May 14 '15 at 20:24
  • i wasn't for sure if it would be onEdit or onUpdate. I'm not editing this sheet and so wasn't sure which option. I will try that. – baldalbino May 14 '15 at 20:49
  • I am editing the first sheet but no the second sheet. It is updating and then I am attempting to move rowns based off of that.- sorry for the confusion – baldalbino May 14 '15 at 21:01
  • Do you want the script to run when you edit the sheet, or do you want to run it manually? Also when you say sheet, do you mean a different spreadsheet or a different sheet in the same spreadsheet? – Akshin Jalilov May 14 '15 at 21:09
  • there is still a problem with: var sheetNameToMoveTheRowTo = (valueToWatch + " Certification") . valueToWatch is an array but should be the verified value which would be one element in the array and the first word in the sheet name of the target sheet. – ScampMichael May 14 '15 at 21:23
  • Oh yeah, I didn't notice that. Try setting the sheet name to value+ certification after the if statement like I showed in my answer. – Akshin Jalilov May 14 '15 at 21:29
  • However, looking at it, I think that there might be a conflict but no bugs are popping up when I attempt to debug it. Here is what I am referring to: var valueToWatch = ["Full", "Partial", "Process", "Conditional", "Fail", "Exemption"]; var sheetNameToMoveTheRowTo = (valueToWatch + " Certification"); – baldalbino May 14 '15 at 21:49
  • compared to : if (valueToWatch.indexOf(cert) === sourceSheet && range.getColumn() === columnNumberToWatch && checkMatch(range.getValue(), valueToWatch) === true) { var sheetNameToMoveTheRowTo = (cert + " Certification"); Do these conflict with each other? – baldalbino May 14 '15 at 21:50
  • You are confusing something. valueToWatch.indexOf(cert) >= 0 and checkMatch(range.getValue(), valueToWatch) === true do the same thing. They both check if a value is present in the array, so valueToWatch.indexOf(cert) === sourceSheet is never going to be true as valueToWatch.indexOf(cert) is either -1 (if value is not present in the array) or more than 0 (if value is present in the array). Again, do I understand correctly that once the data is moved from one spreadsheet to another, you want that data to be automatically placed in the correct sheet, right? – Akshin Jalilov May 14 '15 at 22:01
  • Correct, that is what I want. I wasn't sure about the code – baldalbino May 14 '15 at 22:09
  • I have edited my answer. You can use this script. You no longer need the checkMatch function as valueToWatch.indexOf() does the same check. Try running it with this script. When data is moved from fin to the second spreadsheet, if the value in Column Q matches one of the values in the array, it will move that row to that sheet. Otherwise the row will stay in "Raw Data" sheet. Let me know if you need any further explanation about this :) – Akshin Jalilov May 14 '15 at 22:30
  • Thanks Akshin, you've been a huge help. I am editing the first sheet and it is updating the second sheet but I am still not getting movement after that. I am going to try and dig farther into the code, but I believe we might have gone above my understanding. I have some other codes for this sheet and its possible that one is blocking the other. I will review and let you know if I find anything. – baldalbino May 15 '15 at 13:59
  • That might be the best option. I would need to copy and not delete the row though (just thinking out loud). Let me see if I can update the answer or try the one you updated. Again, thanks for the help – baldalbino May 15 '15 at 18:11
  • In that case just remove the activesheet.deleteRow(row); line. No worries, good luck there – Akshin Jalilov May 15 '15 at 18:22