0

Evening everyone!

I asked this about a week back, but I think the thread got lost in the ether. We came close, but I'm trying to create a function where "Transfer a range of Rows from sheet 1 to sheet 2. Sheet 1 has order IDs in column E. G will have =unique to show me all current order IDs, with check boxes next to each unique reference. Check the box next to which ones you want to CUT over > Select a menu run add on > Run Script > all Rows from A:E that match the desired ID are moved".

[Picture Reference]1

Sheet Reference

function onEdit(e) {
  e.source.toast('Entry')
  const sh = e.range.getSheet();
  if(sh.getName() == "Reference" && e.range.columnStart == 8 &&  e.range.rowStart > 1 && e.value == "TRUE") {
    e.source.toast('Gate1')
    let rg = sh.getRange(e.range.rowStart,1,1,5)
    let vs = rg.getValues();
    const osh = e.source.getSheetByName("Processing");
    osh.getRange(osh.getLastRow() + 1,1,1,5).setValues(vs);
    rg.deleteCells(SpreadsheetApp.Dimension.ROWS);
    e.range.setValue("FALSE");
  }
}

Here is what we had so far. Please let me know if anyone can help, thank you!

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • Kindly add input table and expected output table as **text table** (NOT as IMAGE) to the question. [Click here](https://webapps.stackexchange.com/a/161855/) to create a table easily. Adding such tables greatly increases your chances of getting a elegant answer, as **it is easier to copy/paste**. If you share spreadsheets, your question maybe closed, as questions here must be [self contained](https://meta.stackoverflow.com/a/260455). [Your email address can also be accessed by the public](https://meta.stackoverflow.com/questions/394304/), if you share Google files. – TheMaster Oct 09 '22 at 17:54

2 Answers2

0

To get all rows that match the unique ID whose checkbox was ticked, use Array.filter(), like this:

/**
* Simple trigger that runs each time the user hand edits the spreadsheet.
*
* @param {Object} e The onEdit() event object.
*/
function onEdit(e) {
  if (!e) {
    throw new Error(
      'Please do not run the onEdit(e) function in the script editor window. '
      + 'It runs automatically when you hand edit the spreadsheet.'
      + 'See https://stackoverflow.com/a/63851123/13045193.'

    );
  }
  moveRowsByUniqueId_(e);
}


/**
* Triggers on a checkbox click and moves rows that match a unique ID.
*
* @param {Object} e The onEdit() event object.
*/
function moveRowsByUniqueId_(e) {
  let sheet;
  if (e.value !== 'TRUE'
    || e.range.rowStart <= 1
    || e.range.columnStart !== 8
    || (sheet = e.range.getSheet()).getName() !== 'Reference') {
    return;
  }
  e.source.toast('Moving rows...');
  const uniqueId = e.range.offset(0, -1).getValue();
  const range = sheet.getRange('A2:E');
  const values = range.getValues();
  const targetSheet = e.source.getSheetByName('Processing');
  const _matchWithId = (row) => row[4] === uniqueId;
  const valuesToAppend = values.filter(_matchWithId);
  if (uniqueId && valuesToAppend.length) {
    appendRows_(targetSheet, valuesToAppend);
    range.clearContent();
    const remainingValues = values.filter((row) => !_matchWithId(row));
    range.offset(0, 0, remainingValues.length, remainingValues[0].length)
      .setValues(remainingValues);
    e.source.toast(`Done. Moved ${valuesToAppend.length} rows.`);
  } else {
    e.source.toast('Done. Found no rows to move.');
  }
  e.range.setValue(false);
}

For that to work, you will need to paste the appendRows_() and getLastRow_() utility functions in your script project.

doubleunary
  • 13,842
  • 3
  • 18
  • 51
  • Appologies as well as I'm still learning. Do you have a functioning example where this is applied to I can dissect and understand? (primary to what is meant by pasting the appendRow and getLastRow utility functions). – Michael Suhr Oct 10 '22 at 19:29
  • To use this script, replace the code currently in your `Code.gs` file with the script above. Then append the code at the [appendRows_()](https://webapps.stackexchange.com/a/159426/269219) to the end of the `Code.gs` file. See [Apps Script at Stack Overflow](https://stackoverflow.com/tags/google-apps-script/info). – doubleunary Oct 10 '22 at 20:19
  • Looking at it now, I may have another possible closing thought. In the destination Processing tab, all rows from A:E are moved, and starts from the last blank row when it appends. If on 'Processing', Column :F has populated information that goes further than the last blank row in A:E > It will then start from Col F as the next blank row. How or where would you add the optColumn I believe so that it always appends to the next blank row from A:E, ignoring F and so on? – Michael Suhr Oct 11 '22 at 14:49
  • Try `appendRows_(targetSheet, valuesToAppend, 1)`. Please ask only [one question per post](https://meta.stackexchange.com/a/222741). – doubleunary Oct 11 '22 at 18:47
0

It work almost like asked but :

  • it's using a personal lib (available below)
  • didn't make the part realtiv of removing range and aggregate result, I hope i can add it to the lib some day. However, empty cell are fill with -
  • for an obscure reason, it doesn't like the TRUE/FALSE cell, but work like a charm with 1/0 or any other texte value, regex, ...
  • Additional error handling are to be added if not any match or others possibilites
function onEdit(e){
  console.log(SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Reference").getRange("H3").getValue())
  var tableReference = new TableWithHeaderHelper(SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Reference").getRange("A1").getDataRegion());
  var tableReferenceId = new TableWithHeaderHelper(SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Reference").getRange("G11").getDataRegion());
  var tableProcessing = new TableWithHeaderHelper(SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Processing").getRange("A1").getDataRegion());

  // get value
  var id = tableReferenceId.getTableWhereColumn("Move to Sheet").matchValue(1).getWithinColumn("Unique Filter").cellAtRow(0).getValue(); 

  var tableWithinId = tableReference.getTableWhereColumn("Shipment ID").matchValue(id)
  for(var i=0 ; i < tableWithinId.length() ; i++){
    var rangeRowWithinId = tableWithinId.getRow(i);
    tableProcessing.addNewEntry(rangeRowWithinId);
    for(var cell in rangeRowWithinId.getRange()) cell.setValue("-");
  }
  //reset value
  tableReferenceId.getTableWhereColumn("Move to Sheet").matchValue(1).getWithinColumn("Move to Sheet").cellAtRow(0).setValue(0)
}

See below the app script file you need to create in order to use this utils function: https://github.com/SolannP/UtilsAppSsript/blob/main/UtilsGSheetTableHelper.gs

giff of code in action

  • This is amazing to see in real time! I'm not following what is meant however about the Ultis script file (I'm assuming I need to use your script reference on top of the link reference you provided). Could you explain or if it's easier share your version so I can dissect and learn! – Michael Suhr Oct 10 '22 at 19:16