If you have a loop collecting information and then you want to see all the info collected after, it is best to save the info in an array so that you can go through them one by one after.
So do the following:
Declare your variable outside the loop as follows:
Var poolsNames = []; // empty array
Inside the loop, do the following:
poolsNames.push(result.getResponseText());
After the loop, you should have all the values saved in the array and you can check the length of the array for how many you have as follows: poolsNames.length Hope this helps.
--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/810a657b-0389-4eef-b862-472386f2133en%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/b9c139e2-1b8c-4f09-942a-0d5afc88df67n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/1d54d0eb-4708-4b4d-81f1-c1b31d2a6762n%40googlegroups.com.
You are welcome.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/23475988-16b1-4b04-affc-eea5130c1a81n%40googlegroups.com.
The array right now is just 1 dimensional like this: [ item1, item2, item3 ]
setValues expects a 2 dimensional array being given to it. So it wants this:
[ [ item1, item2, item3 ], [item4, item5, item6] … ] each internal array is written to one row, then the next and so on.
One way to solve this is to change how you build your array.
In the for loop where you collect the items and push in the next value into the array, push it in as an array (as one row).
This will allow you to do as you require.
So here in this loop:
for (i=0; i < poolsNames.length; i++);
{
var PoolsRange = sheet.getRange(1,i);
PoolsRange.setValue([nomsBassins]); // add square brackets around nomsBassins, so it gets pushed in as an array.
}
Now it should work.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/6708f829-9314-46a5-afda-6d7b6a7d3883n%40googlegroups.com.
Julien,
Here is the whole function. Just copy paste it in. I tested it to make sure its working for you.
function parametrage()
{
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('PLANNING DES ACTIVITES');
var ui = SpreadsheetApp.getUi();
var result = ui.prompt(
"CONFIGURATION GÉNÉRALE",
'Entrer le nombre de bassins',
ui.ButtonSet.OK_CANCEL);
var button = result.getSelectedButton();
var text = result.getResponseText();
var nomsBassins = [];
if (button == ui.Button.OK)
for (i = 1; i <= text; i++)
{
var result = ui.prompt(
"CONFIGURATION GÉNÉRALE",
'Nom du bassin n°'+i + ' ?',
ui.ButtonSet.OK_CANCEL);
nomsBassins.push([result.getResponseText()]);
}
sheet.getRange(1,1,nomsBassins.length,1).setValues(nomsBassins);
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/4c01dcfd-0aad-4be1-9981-f3dc24f2ff15n%40googlegroups.com.
Hi Julien,
Yes, would be happy to help. Perhaps it is best to take this offline from the group discussion so that we don’t fill the discussion group with back and forth comments.
I sent you a private email. Please respond to that and we can continue.
From: google-apps-sc...@googlegroups.com <google-apps-sc...@googlegroups.com> On Behalf Of Julien PANEVEL
Sent: September 14, 2022 3:14 PM
To: Google Apps Script Community <google-apps-sc...@googlegroups.com>
Subject: Re: [Apps-Script] Google Sheet Apps Script: Store and retrieve values from a for loop
Georges,
Sorry to disturb you again !
Can you help me again to put a loop in a loop:
My pools have multiple spaces.
In order to create a schedule of activities, i would like to name these spaces in the different swimming pools like the diagram below :
After that, i would like to put the data collected as in the example below :
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/f487377c-deda-4d2f-afee-b400ee9607c0n%40googlegroups.com.