在不同的数据之间插入行 [英] Insert row between different data
问题描述
我是来自意大利的Andrew!
我试图编写一个脚本,在电子表格中的某些数据之间插入空行。 b
$ b
例如,我有这样的数据集:
,我想获得这样的内容:
换句话说:
脚本应检查所有行,并且在日期更改和名称更改时必须添加一个空行。
对此有何想法?
我试图修改一个我在网上找到的脚本,它删除表单中的空白行,但我无法获得任何结果。
感谢!
(:我同意Sandy Good ...就在我头顶,这是一块可以让你开始的代码:
function doSomething(){
var spreadsheet = SpreadsheetApp.openById('yourID' ),//获取电子表格
tab = spreadsheet.getSheets()[0],//获取第一个标签
values = tab.getRange(2,1,tab.getLastRow(),3) .getDisplayValues(),//由于标题
newValues = []获取第二行开始的值,//我们要推新值的空数组
lastDate,
lastName;
//遍历所有值
for(var i = 0; i< values.length; i ++){
//如果姓氏是不同于当前名称或最后日期不同于当前日期a dd如果((lastDate!= undefined&& amp; lastName!=未定义)&& (values [i] [0]!= lastDate || values [i] [1]!= lastName)){
newValues.push(['','','']);
}
//将当前行添加到新数组
newValues.push(values [i]);
//将lastDate和lastName设置为下一次评估的当前值
lastDate = values [i] [0];
lastName = values [i] [1];
}
//设置新值
tab.getRange(2,1,newValues.length,3).setValues(newValues)
}
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:
and I want to obtain something like this:
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!
(: 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)
}
这篇关于在不同的数据之间插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!