t-max
t-max

Reputation: 5

for loop on Text + number on Apps Script

https://i.sstatic.net/9CRFd.png I'm trying to use loop to set value "Hello1" in cell F11, "Hello2" in cell F12, "Hello3" in cell F13, then continue until "Hello10". The Cell F10 already has the word "Hello" on it. So far I only managed to show the value "1" on everything

image.

Here's how I wrote the script:

  function Exercise2() {

  var exercise2 = SpreadsheetApp;
  var activeSheet = exercise2.getActiveSpreadsheet().getActiveSheet();
  var helloCell = activeSheet.getRange(10, 6).getValue();

  activeSheet.getRange(1, 1).moveTo(activeSheet.getRange("F10"));

  for(var x=10;x<21;x++){
  var helloCell = activeSheet.getRange(x, 6).getValue();
  helloCell = helloCell+1;
  activeSheet.getRange(x, 6).setValue(helloCell);
    }

  }

Upvotes: 0

Views: 216

Answers (1)

Martin
Martin

Reputation: 16423

The for loop can be greatly simplified to somthing like this:

for(var x = 1; x < 11; x++) {
    activeSheet.getRange(x + 9, 6).setValue("Hello" + x);
}

This will:

  1. Create a loop that runs from 1 to 11 stored in x.
  2. Set the value of the cell at x + 9 to the value "Hello" & x.

Upvotes: 1

Related Questions