There is a googlesheet with 4 similar tabs (on each, the same table template, but different data) and one tab with a list of the names of the 4 first tabs. Example: https://docs.google.com/spreadsheets/d/1Xdws6Vr6YVXuj19mvpOvkpJokOfY2T7Sh1aMpous_Xw/edit#gid=0
What the script does: goes into each tab and adds a certain number of lines, copying the date from the previous line, increasing it by one month. Where to insert a line is determined by the place where the word "Picture" is written
What caused the problem: inserting rows occurs only for one cell on the sheet where Picture occurs. If Picture occurs 3 times for example, then lines will be inserted in only one place.
How to make the line inserted in all places on the sheet where there is a Picture?
Code (thanks to Ihor Husar for help and understanding):
function loop(howMany){ if(!howMany){howMany = 2;}; var ss = SpreadsheetApp.getActiveSpreadsheet(); var sheets = ss.getSheets();//returns an array of all sheets //this is just a different way of looping through the sheets for (var sheetId = 0; sheetId<sheets.length; sheetId++){ if(sheets[sheetId].getSheetName() != "All_base"){ var data = sheets[sheetId].getDataRange().getValues(); var insertAfter = 6; for(var i = 5; i<data.length; i++){ if(data[i][0] == "Picture"){ insertAfter = i-1; //it is actually -2, but since i starts from 0, it is already smaller by 1; //actual row of Picture cell is i+1, and 2 cells up is i+1-2 = i-1 }; }; sheets[sheetId].insertRowsAfter(insertAfter, howMany); //At this point we inserted rows var lastDate = sheets[sheetId].getRange(insertAfter,1).getCell(1,1).getValue();//the last specified date as a Date object for(var i=0; i<howMany; i++){ var newDate = new Date(lastDate.getTime()); newDate.setMonth(newDate.getMonth()+1+i);//automatically will increase year if the next month is in the next year. sheets[sheetId].getRange(insertAfter+1+i,1).getCell(1,1).setValue(newDate); }; }; }; }