Ask Your Question
1

How to apply the values of a 1x6 array to an _x6 array using Google App Script in Google Sheet?

asked 2022-04-12 11:00:00 +0000

ladyg gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-03-28 17:00:00 +0000

huitzilopochtli gravatar image

You can use a for loop to apply the values of a 1x6 array to an _x6 array in Google App Script. Here is an example code:

function applyValues() {
  var sourceArray = [1, 2, 3, 4, 5, 6]; // 1x6 array
  var targetArray = [['', '', '', '', '', ''], ['', '', '', '', '', '']]; // _x6 array

  for (var i = 0; i < sourceArray.length; i++) {
    for (var j = 0; j < targetArray.length; j++) {
      targetArray[j][i] = sourceArray[i]; // assign each value from source array to each row in the target array
    }
  }

  Logger.log(targetArray); // log the target array to check the result
}

In this example, the sourceArray is a 1x6 array and the targetArray is a 2x6 array (with 2 rows and 6 columns). The goal is to apply the values of the sourceArray to all rows of the targetArray (in the same column).

To do this, we use a nested for loop where the outer loop iterates over the values of the sourceArray, and the inner loop iterates over the rows of the targetArray. Then, we assign each value from sourceArray to the corresponding element of the targetArray (using the same column index i). Finally, we log the target array to check the result.

Note that this code assumes that the size of the targetArray is known (in this case, 2x6). If the size of the targetArray is not known, you can use the push() method to add each row dynamically.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-04-12 11:00:00 +0000

Seen: 10 times

Last updated: Mar 28 '22