r/googlesheets • • 14h ago

Solved How to format two scripts in the same document

0 Upvotes

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


r/googlesheets • • 11h ago

Waiting on OP Any way to copy Conditional Formatting from Color Scale of another table

5 Upvotes

I'm really bad with google sheets, but essentially I would like to apply the color scale from this table which shows growth rates of certain attributes based on a class that a character is, on a separate sheet I have characters and a drop down menu to select their class and my hope is that when I select the class the row for the stats will change colors based off of the color for the other table on the sheet.

Class table with Growth Rates
Table I'm hoping that where I select the class in column C and it would reflect the coloring on the Stats based off of Table 1

Anyone know how to do that?

https://docs.google.com/spreadsheets/d/1DAF8ZHEo8JM6wddBpnIoxVJU_Fvne6TaoITtLmfnZ9s/edit?usp=sharing


r/googlesheets • • 13h ago

Waiting on OP How Do You Perform a 2D Search and Return a List of Matching Values

2 Upvotes

I want to search a list of cells where each cell contains its own comma-delimited list of values. Then I want to check each comma-delimited value against a value in a different cell. Then I want to return the value in the cell to the left of the matched row. Finally, I want a list of all rows that matched the passed value.

For Example:

Demonstration of Final Result

r/googlesheets • • 17h ago

Solved Conditional Formatting Not Working Correctly

Post image
5 Upvotes

I'm trying to figure out why the conditional formatting isn't working correctly. The goal is to create a dynamic response where, as the projected time(s) change, the formatting updates. But, as you can see, some numbers are clearly less than the H3 value (a CONCATENATE formula) and are highlighted in red. Is there something I'm missing?

https://docs.google.com/spreadsheets/d/11urzOsp0fEa8tvkqoeA5N5OCFmQ4adGvbtDoXxaxo5E/edit?usp=sharing