Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

google apps script - Insert row between different data

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

(: 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)

} 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...