0

Hi I'm trying to get a script that will run some code for each name on a list created from my 'sheet1' column 'A2:A'

1. get the list from Column A2:A
2. run code for each name from the list

Here's my code but it's not working properly I don't think the array is populating properly

function test(){
var sheet = SpreadsheetApp.getActive().getSheetByName('sheet1');
var list = sheet.getRange('A2:A').getValues();
var a = [list];
a.forEach(function(entry) {
    
  //do something

 });
}
3
  • 1
    list is already an array - why did you write [list]? stick 'console.log(entry)' in your do something and see what happens. Commented Jan 7, 2021 at 23:36
  • I've change the [list] to just list now I'm getting the list of name but I'm also getting all the empty rows as 4:42:31 PM Info [ '' ] Commented Jan 7, 2021 at 23:45
  • Use getRange(2,1,sheet.getLastRow()-1).getValues() or as some do getRange('A2:A' + sheet.getLastRow()).getValues() not providing a numerical value to the last part often generates null all the way down to maxrows. Some people filter it out with .filter(e=>return e); which utilizes the falsyness of e Commented Jan 8, 2021 at 1:01

3 Answers 3

1

Sample Sheet

function test(){
    var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
    var lastSourceRow = sheet.getLastRow();//get last row
    for (i=2; i<=lastSourceRow; i++){//ignore headers and loop 
      var currentName=sheet.getRange(i, 1).getValue(); //looping column A 
      console.log(currentName)
      //process the name 
    }
    
}
Sign up to request clarification or add additional context in comments.

2 Comments

That's kind of a slow way to read the data. You might wish to read Best Practices and pay special attention to the setValues() method
Thanks mxLiew I tried you code and it's doing exactly what I needed
1

You can flatten the list array and then iterate over each value directly.

Code snippet:

function test(){
var sheet = SpreadsheetApp.getActive().getSheetByName('sheet1');
var list = sheet.getRange('A2:A'+sheet.getLastRow()).getValues().flat(); // modified code
list.forEach(entry=> { 
  //do something with entry
  console.log(entry);
 });
}

I use an arrow function but you can use your current solution as well.

3 Comments

Hi Mario I tried your code as a test for the 'do something with entry' I'm trying to insert the entry in sheet1 cell D2 with a 3 sec pause before changing to the next entry. The console.log (entry) works properly but I can't get it to set the name in cell D2
@YvanL So you want to write a new value every 3 seconds? You didn't mention this in your question. You can use sleep for that. Also, how are you trying to insert the entry? Do you want to write all these values to D2:D, waiting 3 seconds between the writing of each cell?
@Iamblichus what is more weird is that the accepted answer does not do what the OP describes just now... besides the fact that it is the most inneficient approach. Anyway..
0
function loopthroughnamesfoundinsheet(){
  const ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName('sheet1');
  var list = sheet.getRange(2,1,sheet.getLastRow()).getValues().flat();//flattened array      
  list.forEach((name,i)=> { 
    let sh=ss.getSheetByName(name);//I added code here to show you how to write data to 'D2' in each sheet
    sh.getRange('D2').setValue(i+2);
    //another thing you might like to do is to copy a bunch of data all at once
    sh.getRange(2,2,list.length,list[0].length).setValues(list);//this copies the list in Sheet1 into every sheet in the list.
    console.log(name);
  });
  SpreadsheetApp.flush();//will force all pending calculations on the spread to complete and data will appear afters on the spreadsheet.
}

2 Comments

thanks for your answer Cooper the console log is showing the proper result but when I'm trying to do something with the name like inserting the name in sheet1 cell D2 wait 3 sec then insert the next name nothing show up in cell D2
I modified the script for you. It takes the name and using the getSheetByName() method it gets the sheet and then using the getRange('D1') method I set the value i+2 in 'D2' of every sheet in the list i+2 in the row number of each name in the list in Sheet1. If your going to write and read data immediately then it is advisable to use SpreadsheetApp.flush() in between those two operations but generally we try to avoid that because it's a bit time consuming and cuts down your efficiency.

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.