0

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);
    }

  }

1 Answer 1

1

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.
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for taking the time to look into it however, by following this I only got zero on everything. Basically, what I'm trying to get at is this i.sstatic.net/9CRFd.png.
I think I made a mistake in using the VBA-style syntax for string concatenation which is why you are getting 0 instead of text. I'll edit the question, but "Hello " & x should be "Hello" + x (note the + instead of &).

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.