此代码将查看A列(跳过标题)并执行此评估:
如果单元格值不等于下一个单元格值,则执行此操作:
( 30 (this is a static number that won't change) - (whatever the cell count is at that point) = X
在最后一个相等的单元格下面插入X行。 (所以现在在单元格之间存在X个空行不相等。
***我认为下一个规则可能更适合在最后使用这个操作:
代码:
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process, assuming headers
var startColumn = 1; // Last row of data to process
var lastRow = sheet.getLastRow(); // Number of rows to process
var lastColumn = sheet.getLastColumn();
// Fetch the range of cells where data exist
var dataRange = sheet.getRange(startRow, startColumn, lastRow, lastColumn)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
var j = 0;
for (i in data) {
var row = data[i];
var nextRow = data[i+1];
var prevRow = data[i-1];
var bin = row[1];
var nextBin = nextRow[1];
var prevBin = prevRow[1];
if (oldBin == prevBin) {
j++
}
if (nextBin != bin && nextBin != "empty" && j>=29) {
var j = 0;
}
else if (nextBin != bin && nextBin != "empty") {
sheet.insertRowAfter(i);
sheet.getRange(i + 1, 1).setValue(”empty") //marking the row as “empty"
}
} //End “for”
答案 0 :(得分:0)
根据我对你问题的理解:
1.您想检查某些列的值(此处为A但您的代码是指B列)
2.将每个单元格值与下一行单元格值进行比较。如果值相等,则增加计数,或者如果计数值> 1,则在行之后插入等于行数的行数。 30并将计数设为0。
3.一旦插入行,请将“空”值添加到新插入行的单元格中。
请找到下面的代码,找出不匹配的单元格并插入行:
function countRows()
{
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process, assuming headers
var startColumn = 1;
var lastRow = sheet.getLastRow(); // Number of rows to process
var lastColumn = sheet.getLastColumn();
// Fetch the range of cells where data exist
var dataRange = sheet.getRange(startRow, startColumn, lastRow, lastColumn)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
var j = 0, k = 0;
var rows = [];
for (var i = 0; i < data.length-1; i++)
{
var row = data[i];
var nextRow = data[i + 1];
var bin = row[1];
var nextBin = nextRow[1];
if (bin == nextBin) { j++ }
else
{
rows.push([i+1,j]);
j=0;
}
}
var temp = 1;
for (i=0; i< rows.length; i++)
{
var data = rows[i];
if(data[1] > 30){
sheet.insertRowsAfter(data[0]+ temp, data[1]);
temp += data[1];
}
}
}
现在插入了所有行。您只需将“空”字符串值设置为插入的行。
希望有所帮助!