1

I'm Andrew from Italy!

I'm trying to write a script that inserts a blank row between some data in a spreadsheet.

Example, I have this data set:

starting data set

and I want to obtain something like this:

final data set

In other words:

The script should check all the rows and, when the date change and the name change, must add an empty row.

Any idea about this? I've tried to modify a script that I've found online that remove blank rows in a sheet but I'm not able to get any results.

Thanks!

Andrea Volontè
  • 85
  • 1
  • 3
  • 10
  • 1
    I would get all the data, process it, build a new data set with blank rows, then overwrite the original data. – Alan Wells Jul 22 '16 at 23:03
  • @SandyGood thanks for your suggestione. I'm trying to write something that compare the cell content with the previous one and, if different, add a blank row. I'm not good in coding. Do you have some example to start with? – Andrea Volontè Jul 23 '16 at 07:55

1 Answers1

3

(: I agree with Sandy Good... Just of the top of my head, here's a piece of code that can get you started:

function doSomething() {
 var spreadsheet = SpreadsheetApp.openById('yourID'), // Get the spreadsheet
     tab = spreadsheet.getSheets()[0], // Get the first tab
     values = tab.getRange(2, 1, tab.getLastRow(), 3).getDisplayValues(), //Get the values beginning in the second row because of the headers
     newValues = [], // Empty array where we're gonna push the new values
     lastDate,
     lastName;

 // Loop through all the values
 for(var i = 0; i <values.length; i++){
   // If the last name is different from the current name or the last date is different from the current date add an empty "row" to the new array
   if((lastDate != undefined && lastName != undefined) && (values[i][0] != lastDate || values[i][1] != lastName)){
     newValues.push(['','','']);
   }

   //Add the current row to the new array
   newValues.push(values[i]);

   //Sets the lastDate and lastName to the current values for the next evaluation
   lastDate = values[i][0];
   lastName = values[i][1];
 }

 //Sets the new values
 tab.getRange(2,1,newValues.length,3).setValues(newValues)

} 
ocordova
  • 1,788
  • 2
  • 13
  • 20
  • Thanks! This is a good solution! What if I want to dinamically change the "push argument" based on the number of column? I tried with a for cycle (that write ", ''" many times the number of columns) but it don't work. I think I can not push a variable into an array. Any suggestion? – Andrea Volontè Oct 06 '16 at 13:15
  • @AndreaVolontè you could figure out tab.getLastColumn() and then create an array of blank values based on that. See https://stackoverflow.com/a/10451941/444921 for an example. – Rob Barreca Jan 06 '20 at 19:35