Quantcast
Channel: Hot Weekly Questions - Web Applications Stack Exchange
Viewing all articles
Browse latest Browse all 9712

Script setValue in column based on the values of two columns with a case/switch and for-loop

$
0
0

I'm working on a Google Sheets document in which I calculate profits based on two columns: Price and payment method. I'd like help with a script that checks the values of said columns in each row, and based on those two columns sets a certain value in another column (net profit).

So: If row 2 column E (price) = €47,00 && column H (payment menthod) = "iDEAL" then set the value in column K (net profit) of row 2 to €42,98.

My table:

Example of my table


Text version of sample data

DateTimeOrder_nrProductPriceVatEmailPayment methodCouponcodeCouponamountNet profit
12/12/202001:02:03order_wc_order_nr_2Awesome Product$47.003.88customer@email.comIDEAL
14/12/202002:05:04order_wc_order_nr_3Awesome Product$0.000customer@email.com43.12
16/12/202003:10:05order_wc_order_nr_4Awesome Product$28.202.33customer@email.comBancontact17.25
17/12/202004:15:06order_wc_order_nr_5Awesome Product$47.003.88customer@email.comIDEAL
23/12/202005:20:07order_wc_order_nr_6Awesome Product$47.003.88customer@email.comIDEAL
23/12/202006:25:08order_wc_order_nr_7Awesome Product$47.003.88customer@email.comIDEAL
23/12/202007:30:09order_wc_order_nr_8Awesome Product$47.003.88customer@email.comIDEAL
23/12/202008:35:10order_wc_order_nr_9Awesome Product$47.003.88customer@email.comIDEAL
23/12/202009:40:11order_wc_order_nr_10Awesome Product$47.003.88customer@email.comPayPal
23/12/202010:45:12order_wc_order_nr_11Awesome Product$47.003.88customer@email.comIDEAL
23/12/202011:50:13order_wc_order_nr_12Awesome Product$47.003.88customer@email.comIDEAL
23/12/202012:55:14order_wc_order_nr_13Awesome Product$47.003.88customer@email.comIDEAL
23/12/202014:00:15order_wc_order_nr_14Awesome Product$47.003.88customer@email.comIDEAL

This table has 3 different price values: €47, €37,9 and €28,20.We offer four different payment methods: "iDEAL", "PayPal", "Bancontact" and "Creditcard".

For each combination of price value and payment method, there is a different net profit.
Net profit values:


Table2 data as text

$47.00Net Profit
IDEAL$42.89
PayPal$41.09
CreditCard$42.25
BanConnect$42.81
$37.60Net Profit
IDEAL$34.27
PayPal$32.47
CreditCard$33.63
BanConnect$34.19
$28.20Net Profit
IDEAL$25.64
PayPal$23.84
CreditCard$25.00
BanConnect$25.56

I tried to edit the following script:

function onEdit() {  var sheetActive = SpreadsheetApp.openById("mySheetID");  var s = sheetActive.getSheetByName("mySheetName");  var r = s.getActiveCell();      if( r.getColumn() == 7 && r.getValue() == 47)      var nextCell = r.offset(0, 2);      nextCell.setValue(42.89);}

Based on the script in this link: How do I update one column based on the value of another using a script?

But I didn't get it to work and it also doesn't take the values of two columns into account.

I also found the following script: Use `setValue` to update a column based on values of two others that match their respective values in reference cells

But I haven't been able to find out how to edit the script to fit my needs.

I have minimal experience with scripting and programming but I think I need a for-loop that iterates through my rows in combination with a case/switch based on two column values.

Please help me out and thanks in advance!


Viewing all articles
Browse latest Browse all 9712

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>