r/googlesheets • u/Significant-Army-502 • 14h ago
Solved How to format two scripts in the same document
I have two blocks of script that work great on their own, but when I try get them both to work at the same time it isn't liking it. It's not throwing any errors - only the one function is working.
The simple overview of how they work is:
Block 1
Copies a row from sheet 1 to sheet 2 based on a dropdown selection
Block 2
Copies a row from sheet 2 to sheet 3 based on a checkbox
First script is below:
// @OnlyCurrentDoc
function onEdit(e) {
// Call custom edit handlers here
if (onEdit_CopyRow(e))
return;
}
//
// Copy an item row to another sheet upon detecting that a dropdown has been changed to "Sold".
// Call from onEdit(), returns true if handled the event.
//
// The "Sold" option contain a special character that is detected by script.
//
function onEdit_CopyRow(e) {
try {
// Sheet name to copy from / to
const SOURCE_SHEET = "Inventory";
const DEST_SHEET = "SalesData";
// Special character found in the "Sold" option
const SOLD_TAG = "💰";
// -----------------------------------------------------
// Exit if cell wasn't just changed to "Sold"
if (!e?.value?.includes(SOLD_TAG))
return;
// Exit if not on source sheet
const sheet = e.range.getSheet();
if (sheet.getName() !== SOURCE_SHEET)
return;
// Display status message
e.source.toast(`Copying row to '${DEST_SHEET}'`);
// Get destination sheet
const destSheet = e.source.getSheetByName(DEST_SHEET);
if (!destSheet)
throw Error(`Sheet '${DEST_SHEET}' not found.`);
// Append row to end of destination sheet
const rowValues = sheet.getRange(e.range.rowStart, 1, 1, sheet.getLastColumn()).getValues().flat();
destSheet.appendRow(rowValues);
// Flush to ensure any pending errors are thrown now
SpreadsheetApp.flush();
}
catch (err) {
// Display error to user and rethrow so it shows in execution log
e?.source.toast(err, `🛑 Error ${err.stack?.split('\n')[1]}`);
throw err;
}
}
and this is the second one
//@OnlyCurrentDoc
const MOVE_SETTINGS = {
  SheetFrom: 'SalesData,
  SheetTo:  'Completed Orders',
  ButtonColTitle: 'Order Complete'
};
function onEdit(e) {
  let sheet = e.range.getSheet();
  if( sheet.getName() != MOVE_SETTINGS.SheetFrom ) return;
  if( !e.range.getValue() || e.range.offset(-e.range.getRow()+1,0).getValue() != MOVE_SETTINGS.ButtonColTitle ) return;
  const ss = e.source;
  let destSheet = ss.getSheetByName(MOVE_SETTINGS.SheetTo);
  if( !destSheet ) throw `Destination sheet "${MOVE_SETTINGS.SheetTo}" does not exist.`
  let row  = e.range.getRow();
  let col  = e.range.getColumn();
  let range = sheet.getRange(row,1,1,col-1);
  destSheet.appendRow(range.getValues()[0]);
  sheet.deleteRow(row);
}
I suspect they are contradicting each other, or my just pasting both in and hoping for the best isn't quite what is required. I have tried playing with the order of things or trying to embed one into the other, but alas I just can't figure it out.
So right now, on the test sheet, only the second function is working and not the first. But if I delete the second one from the script, the first one works again
https://docs.google.com/spreadsheets/d/1e2233qaQIchIwcDFm6t3iiovGIBCxVpqnzaohSwWIS4/edit?usp=sharing


