Skip to main content

Hey Zapier Community! I was hoping someone can help with my Zap, I’m looking to conslidate similar line items. Happy to donate a coffee for anyone who can help :)

Current Zap:

  • Step 1: Lookup Spreadsheet Rows (output as Line Items) in Google Sheets - it’s pulling a list of drinks (we’re a small events business)
    • Output is: 
      YB Prosecco x 1  Water x 1,Prosecco x 1 Water x 1,Coke No Sugar x 1 Water x 1,Coke No Sugar x 1 Water x 1,YB Prosecco x 1 Water x 1
  • Step 2: Formattter - Utilties - Line-item to Text, seperator is “,”
    • Output is:
      YB Prosecco x 1
      Water x 1,Prosecco x 1
      Water x 1,Coke No Sugar x 1
      Water x 1,Coke No Sugar x 1
      Water x 1,YB Prosecco x 1
      Water x 1
  • Step 3: Formatter - Text - Repalce “,” with l:newline:]
    • Output is: 

      YB Prosecco x 1

      Water x 1

      Prosecco x 1

      Water x 1

      Coke No Sugar x 1

      Water x 1

      Coke No Sugar x 1

      Water x 1

      YB Prosecco x 1

      Water x 

  • Step 4: Email: send output from step 3 to our internal staff.

 

I would love to consolidate the list from step 3 and create a total for each type of drink.

  • Prefered Output:
    YB Prosecco x 2
    Water x 4
    Prosecco x 1
    Coke No Sugar x 2

Is this possible? I’ve spoken to Zapier Support who suggest this could be achieved with custom code - perhaps setups 2 and 3 could be cut out to save on the number of zaps. Happy to donate a coffee for anyone who can help :)

@wandrew 

Here’s the python script from chatgpt for this that you can use to aggerate the result: you will need to change the input and output format, also not sure if the library is supported

from collections import Counter

def aggregate_data(data):
data_list = data.strip().split('\n')
data_count = Counter(data_list)
return '\n'.join(of"{item} x {count}" for item, count in data_count.items()])

data = "YB Prosecco x 1\nWater x 1\nProsecco x 1\nWater x 1\nCoke No Sugar x 1\nWater x 1\nCoke No Sugar x 1\nWater x 1\nYB Prosecco x 1\nWater x 1"
print(aggregate_data(data))

 


@robschmidt

thank you for your help, I will give that a try.

Sorry I forgot to mention:

- The is triggered every day at 10 am

- The type of drinks and the number of drinks pulled from Google Sheets will be different every day. We have 20 different types of drink on the menu

 

Thank you for your patience, this is my first time posting


Hi @wandrew 

yes, code would work great for this. The below solution uses a Javascript Code block and assumes you’re passing the output from step 3 (separated by new lines) and consolidates exact matches in the name. 

 

let data = inputData.data.split("\n").filter(line => line !=""); //split data at new line character & filter out empty lines

let consolidated_data = {}

data.forEach(line => {
let temp = line.split(" x "); //split up at the x
let name = temp=0] //name is first part
let qty = parseInt(tempt1]) //qty is the second part;
if (consolidated_datadname]) {
//we've seen this drink before, sum qty
consolidated_datadname] += qty;
} else {
// new drink
consolidated_datadname] = qty;
}
})

let message = "";
for (const ckey, value] of Object.entries(consolidated_data)) {
//all data has been consolidated, rebuild items for message
message += key + " x " + value + "\n";

}

return {message, consolidated_data}

 


@GetUWired That worked - that you so much! I’m very impressed. I owe you a coffee!


Happy to help!