我怎么能得到每一行?

时间:2016-10-13 17:58:04

标签: javascript google-apps-script google-sheets

我试图获取然后删除电子表格中的每一个空白行。如果包含值的行包含空白单元格,我也想删除该行。

我不想只删除那些带有最后一行的行的行,我想删除每一个空白行。

例如,

-1 2 3 4 5
-1 2 3 4 5
-          << delete row
-1 2 3 4 5
-          << delete row
-          << delete row
-1 2 3 4 5
-1 2   4 5 << delete row -- cell is blank
-          << delete row

.getDataRange似乎没有得到空行,那么我将如何获取for循环中的所有行?

修改

var artistFile = SpreadsheetApp.openById(fileId); //open artists spreadsheet
var artistFileRows = artistFile.getActiveSheet().getMaxRows(); //get number of rows in spreadsheet -- 1 = no values (only headers are in the sheet), 2+ = values (headers + values are in the sheet)

Logger.log(artistFileRows);
var startRange = "A1";
var endRange = "F" + artistFileRows;
var totalRange = startRange + ":" + endRange;
Logger.log(totalRange);
var allValues = artistFile.getRange(totalRange).getValues();

for (var a = 0; a < allValues.length; a++)
{
  Logger.log("row " + parseInt(a + 1) + ": " + allValues[a]);

  if (allValues[a] === '')
  {
    Logger.log("true");
  }
  else
  {
    Logger.log("false");
  }
}
var artistFile = SpreadsheetApp.openById(fileId); //open artists spreadsheet
var artistFileRows = artistFile.getActiveSheet().getMaxRows(); //get number of rows in spreadsheet -- 1 = no values (only headers are in the sheet), 2+ = values (headers + values are in the sheet)

Logger.log(artistFileRows);
var startRange = "A1";
var endRange = "F" + artistFileRows;
var totalRange = startRange + ":" + endRange;
Logger.log(totalRange);
var allValues = artistFile.getRange(totalRange).getValues();

for (var a = 0; a < allValues.length; a++)
{
  //Logger.log("row " + parseInt(a + 1) + ": " + allValues[a]);

  for (//iterate through each value in the row)
  {
    Logger.log(//each value in the row);
  }
}

代码:

for (var a = 0; a < allValues.length; a++)
{
  //Logger.log("row " + parseInt(a + 1) + ": " + allValues[a]);

  for (var b = 0; b < allValues[a].length; b++)
  {
    Logger.log(allValues[a][b]);

    if (allValues[a][b] === '')
    {
      Logger.log("true");
      //artistFile.deleteRow(a);
    }
  }
}

执行记录:

[16-10-13 19:24:54:885 EDT] 10.0
[16-10-13 19:24:54:885 EDT] A1:F10
[16-10-13 19:24:54:917 EDT] Date
[16-10-13 19:24:54:918 EDT] h1
[16-10-13 19:24:54:918 EDT] h2
[16-10-13 19:24:54:919 EDT] h3
[16-10-13 19:24:54:919 EDT] h4
[16-10-13 19:24:54:920 EDT] h5
[16-10-13 19:24:54:921 EDT] d2
[16-10-13 19:24:54:921 EDT] td2
[16-10-13 19:24:54:922 EDT] ca2
[16-10-13 19:24:54:922 EDT] cr2
[16-10-13 19:24:54:923 EDT] t2
[16-10-13 19:24:54:923 EDT] ns2
[16-10-13 19:24:54:924 EDT] d3
[16-10-13 19:24:54:924 EDT] td3
[16-10-13 19:24:54:924 EDT] ca3
[16-10-13 19:24:54:925 EDT] cr3
[16-10-13 19:24:54:925 EDT] t3
[16-10-13 19:24:54:926 EDT] ns3
[16-10-13 19:24:54:926 EDT] 
[16-10-13 19:24:54:926 EDT] true
[16-10-13 19:24:54:927 EDT] 
[16-10-13 19:24:54:927 EDT] true
[16-10-13 19:24:54:927 EDT] 
[16-10-13 19:24:54:928 EDT] true
[16-10-13 19:24:54:928 EDT] 
[16-10-13 19:24:54:929 EDT] true
[16-10-13 19:24:54:929 EDT] 
[16-10-13 19:24:54:929 EDT] true
[16-10-13 19:24:54:930 EDT] 
[16-10-13 19:24:54:930 EDT] true
[16-10-13 19:24:54:930 EDT] 
[16-10-13 19:24:54:931 EDT] true
[16-10-13 19:24:54:931 EDT] 
[16-10-13 19:24:54:932 EDT] true
[16-10-13 19:24:54:932 EDT] 
[16-10-13 19:24:54:932 EDT] true
[16-10-13 19:24:54:933 EDT] 
[16-10-13 19:24:54:933 EDT] true
[16-10-13 19:24:54:933 EDT] 
[16-10-13 19:24:54:934 EDT] true
[16-10-13 19:24:54:934 EDT] 
[16-10-13 19:24:54:934 EDT] true
[16-10-13 19:24:54:935 EDT] d4
[16-10-13 19:24:54:935 EDT] td4
[16-10-13 19:24:54:936 EDT] ca4
[16-10-13 19:24:54:936 EDT] cr4
[16-10-13 19:24:54:936 EDT] t4
[16-10-13 19:24:54:937 EDT] ns4
[16-10-13 19:24:54:937 EDT] test1
[16-10-13 19:24:54:938 EDT] test1
[16-10-13 19:24:54:938 EDT] test1
[16-10-13 19:24:54:938 EDT] test1
[16-10-13 19:24:54:939 EDT] test1
[16-10-13 19:24:54:939 EDT] test1
[16-10-13 19:24:54:940 EDT] 
[16-10-13 19:24:54:940 EDT] true
[16-10-13 19:24:54:940 EDT] 
[16-10-13 19:24:54:941 EDT] true
[16-10-13 19:24:54:941 EDT] 
[16-10-13 19:24:54:941 EDT] true
[16-10-13 19:24:54:942 EDT] 
[16-10-13 19:24:54:942 EDT] true
[16-10-13 19:24:54:943 EDT] 
[16-10-13 19:24:54:943 EDT] true
[16-10-13 19:24:54:943 EDT] 
[16-10-13 19:24:54:944 EDT] true
[16-10-13 19:24:54:944 EDT] test2
[16-10-13 19:24:54:945 EDT] test2
[16-10-13 19:24:54:945 EDT] test2
[16-10-13 19:24:54:945 EDT] test2
[16-10-13 19:24:54:946 EDT] test2
[16-10-13 19:24:54:946 EDT] test2
[16-10-13 19:24:54:946 EDT] 
[16-10-13 19:24:54:947 EDT] true
[16-10-13 19:24:54:947 EDT] 
[16-10-13 19:24:54:948 EDT] true
[16-10-13 19:24:54:948 EDT] 
[16-10-13 19:24:54:948 EDT] true
[16-10-13 19:24:54:949 EDT] 
[16-10-13 19:24:54:949 EDT] true
[16-10-13 19:24:54:949 EDT] 
[16-10-13 19:24:54:950 EDT] true
[16-10-13 19:24:54:950 EDT] 
[16-10-13 19:24:54:951 EDT] true

1 个答案:

答案 0 :(得分:1)

从最后一行开始并向后迭代或从第一行开始,并在每次执行删除时将迭代器减1。

var startRange = "A1";
var endRange = "F" + artistFileRows;
var totalRange = startRange + ":" + endRange;
var allValues = artistFile.getRange(totalRange).getValues();

for (var a = 0; a < artistFileRows; a++)
{
   for (var n = 0; n < allValues[a].length; n++)
   {
      if(allValues[a][n] === '') 
     {
             artistFile.getActiveSheet().deleteRow(a+1);
             a--;
             break;
     }
   }


}