如何逐个获取多行并检查其值?

时间:2016-10-16 02:38:35

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

我正在努力实现的目标:

  • 获取电子表格中的每一行,从第一行开始到最后一行结束
  • 获取该行中的值
  • 将值与''
  • 进行比较
  • 如果该行中有任何值=='',请删除该行,然后继续下一行
  • 否则继续下一行并重复

这是我到目前为止所拥有的。我可以获取电子表格中的每一行及其值。我不确定如何逐行获取行,然后在检查行值后移动到下一行。

var artistFile = SpreadsheetApp.openById(fileId); //open spreadsheet
var artistFileRows = artistFile.getActiveSheet().getMaxRows(); //get number of rows in spreadsheet

var startL = "A"; //start column
var endL = "F"; //end column

//get row one by one
for (var a = 0; a < artistFileRows; a++)
{
  var range = startL + parseInt(a + 1) + ":" + endL + parseInt(a + 1); //range = A1:AX -- X == last row number in spreadsheet

  //get rows values
  var values = artistFile.getRange(range).getValues();

  //compare rows values to ''

  //delete rows with blank values

  //move to next row and repeat
}

这给我一个

的日志
[16-10-14 20:50:34:622 EDT] row: 1, range: A1:F1
[16-10-14 20:50:34:646 EDT] [[header1, header2, header3, header4, header5, header6]]
[16-10-14 20:50:34:647 EDT] row: 2, range: A2:F2
[16-10-14 20:50:34:671 EDT] [[d2, td2, ca2, cr2, t2, ns2]]
[16-10-14 20:50:34:672 EDT] row: 3, range: A3:F3
[16-10-14 20:50:34:695 EDT] [[d3, td3, ca3, cr3, t3, ns3]]
[16-10-14 20:50:34:696 EDT] row: 4, range: A4:F4
[16-10-14 20:50:34:722 EDT] [[, , , , , ]]
[16-10-14 20:50:34:722 EDT] row: 5, range: A5:F5
[16-10-14 20:50:34:748 EDT] [[, , , , , ]]
[16-10-14 20:50:34:748 EDT] row: 6, range: A6:F6
[16-10-14 20:50:34:774 EDT] [[d4, td4, ca4, cr4, t4, ns4]]
[16-10-14 20:50:34:775 EDT] row: 7, range: A7:F7
[16-10-14 20:50:34:799 EDT] [[test1, test1, test1, test1, test1, test1]]
[16-10-14 20:50:34:799 EDT] row: 8, range: A8:F8
[16-10-14 20:50:34:825 EDT] [[, , , , , ]]
[16-10-14 20:50:34:825 EDT] row: 9, range: A9:F9
[16-10-14 20:50:34:849 EDT] [[test2, test2, test2, test2, test2, test2]]
[16-10-14 20:50:34:850 EDT] row: 10, range: A10:F10
[16-10-14 20:50:34:874 EDT] [[t, , t, , , ]]
[16-10-14 20:50:34:874 EDT] row: 11, range: A11:F11
[16-10-14 20:50:34:899 EDT] [[, , , , , ]]

修改

日志:

[16-10-19 01:28:52:863 EDT] Spreadsheet.deleteRow([3]) [0.06 seconds]
[16-10-19 01:28:52:914 EDT] Spreadsheet.deleteRow([4]) [0.051 seconds]
[16-10-19 01:28:52:967 EDT] Spreadsheet.deleteRow([7]) [0.053 seconds]
[16-10-19 01:28:52:989 EDT] Spreadsheet.deleteRow([9]) [0.021 seconds]
[16-10-19 01:28:52:992 EDT] Execution failed: Those rows are out of bounds.

代码:

var count = 0;
var index = [];

for (var i = 0; i < allValues.length; i++)
{
  for (var j = 0; j < allValues[i].length; j++)
  {
    Logger.log(allValues[i][j]);
    if (allValues[i][j] === "")
    {
      index[count] = i;
      count++;
      break;
    }
  }
}

for (var i = 0; i < count; i++)
{
  artistFile.deleteRow(index[i]);
}

编辑2

我修复了显示错误行被删除的.deleteRow()。 for循环以var i = 0开头,但电子表格中的行从第1行开始。我将.deleteRow()更改为artistFile.deleteRow(parseInt(index[i] + 1));

现在显示需要删除的正确行:

[16-10-19 01:40:01:353 EDT] Spreadsheet.deleteRow([4]) [0.053 seconds]
[16-10-19 01:40:01:406 EDT] Spreadsheet.deleteRow([5]) [0.052 seconds]
[16-10-19 01:40:01:461 EDT] Spreadsheet.deleteRow([8]) [0.054 seconds]
[16-10-19 01:40:01:484 EDT] Spreadsheet.deleteRow([10]) [0.023 seconds]
[16-10-19 01:40:01:488 EDT] Execution failed: Those rows are out of bounds.

错误前的最后一行应显示.deleteRow([11])

但是,我仍然收到错误 - Execution failed: Those rows are out of bounds.

3 个答案:

答案 0 :(得分:2)

这肯定有用:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var data = sheet.getDataRange().getValues();

  var count = 0;
    var index = [];

//find the rows which contain
for(var i=0;i<data.length;i++)
{
   for(var j=0;j<data[i].length;j++)
   {
   Logger.log(" Row : "+i+" Column : "+j);
      if(data[i][j]=="" || data[i][j]==" ")
      {
         index[count]=i+1;
         Logger.log(index[count]);
         count++;
         break;
      }
   }
}

//delete the rows
for(var i=0;i<count;i++)
{
   sheet.deleteRow(index[i]-i);
   Logger.log("Row "+index[i]+" deleted");
}

答案 1 :(得分:1)

以下是代码:

var artistFile = SpreadsheetApp.openById(fileId); //open spreadsheet
var sheet = artisiFile.getSheetByName(SheetName); //open the sheet where the data is stored
var data = sheet.getDataRange().getValues();
var count = 0;
var index = [];

//find the rows which contain "
for(var i=0;i<data.length;i++)
{
   for(var j=0;j<data[i].length;j++)
   {
      if(data[i][j]=="\"")
      {
         index[count]=i;
         count++;
      }
   }
}

//delete the rows
for(var i=0;i<count;i++)
{
   sheet.deleteRow(index[i]);
}

答案 2 :(得分:1)

希望这会有所帮助:

 var artistFile = SpreadsheetApp.openById(fileId); //open spreadsheet
    var sheet = artisiFile.getSheetByName(SheetName); //open the sheet where the data is stored
    var data = sheet.getDataRange().getValues();
    var count = 0;
    var index = [];

//find the rows which contain "
for(var i=0;i<data.length;i++)
{
   for(var j=0;j<data[i].length;j++)
   {
      if(data[i][j]=="")
      {
         index[count]=i;
         count++;
         break;
      }
   }
}

//delete the rows
for(var i=0;i<count;i++)
{
   sheet.deleteRow(index[i]);
}

PS。我只做了两次改动。

1)用"\""替换""(因为我误解了要求中的细节)

2)添加了break;语句(这将确保不删除不删除的行)