0

for(var i=0; i<values.length; i++){
    var decisionCol = values[i][29];
    if (decisionCol == ""){
      orderDate = values[i][0];
      orderNo = values[i][1];
      orderRevenue = values[i][16];
    } else {
      orderDate = values[i][11];
      orderNo = values[i][6];
      orderRevenue = values[i][30];
    }
    output.appendRow([output.getLastRow(), orderDate, orderNo, orderRevenue, prodPrice]);
  }

I have google apps code above but it looks ugly. How do I make it shorter and concise?

2 Answers 2

1

Are you familiar with for-of loops? Changing for(var i=0; i<values.length; i++) to for (const value of values) will certainly make your code cleaner and eliminate the 2-dimensional array references.

for (const value of values){
    var decisionCol = value[29];
    if (decisionCol == ""){
      orderDate = value[0];
      orderNo = value[1];
      orderRevenue = value[16];
    } else {
      orderDate = value[11];
      orderNo = value[6];
      orderRevenue = value[30];
    }
    output.appendRow([output.getLastRow(), orderDate, orderNo, orderRevenue, prodPrice]);
  }

Beyond this change, I don't know if there are other changes I would recommend. Your code snippet is already quite short, so it is hard to recommend further refinements. Your code is very easy to understand and follow what is going on. That's a good thing! Attempts to get creative to be more concise might make your code shorter but could also make it harder to follow.

Sign up to request clarification or add additional context in comments.

Comments

1

Modification points:

  • When appendRow() is used in a loop, the process cost becomes high. Ref
    • I would like to propose to put the values using setValues().
  • In your situation, getLastRow() can be used at outside of the loop.

When your script is modified using above points, it becomes as follows.

Modified script:

var lastRow = output.getLastRow();
var res = values.map(r => [lastRow++, r[29] == "" ? [r[0], r[1], r[16]] : [r[11], r[6], r[30]], prodPrice].flat());
output.getRange(output.getLastRow() + 1, 1, res.length, res[0].length).setValues(res);

Note:

  • In this modified script, it supposes that the variables of values, output and prodPrice are correctly declared. Please be careful this.

References:

3 Comments

Hi Tanaike. Please try to refrain from adding "Please think of this as just one of many possible answers" to your answers. Readers already know this (it is pretty obvious). You need not worry that readers will religiously use your material just on the basis that you have provided it. It is always the reader's responsibility to check the suitability of material they find on the internet.
@halfer Thank you for your advice. And I deeply apologize. I understood your advice.
You are welcome. As ever, apologies not necessary 👍

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.