【问题标题】:Google Apps Script - Find Last Empty Row of a Column in SpreadsheetGoogle Apps 脚本 - 在电子表格中查找列的最后一个空行
【发布时间】:2017-02-13 00:31:24
【问题描述】:

我目前有一个这样的脚本:(每分钟运行一次并获取任何值)

  // function 01
  sheet2.getRange(sheet2.getLastRow() + 1, 1, 1, 6).setValues(values);

  // function 02
  sheet2.getRange(sheet2.getLastRow() + 1, 10, 1, 6).setValues(values);

它找到最后一行并在下一行设置值。两者都在不同的功能中。但目前它输出的是这样的。

电流输出:不好

// function 1 output here       // function 2 output here
------------------------------------------------------------
|   A    |     B    |   C     ||         |         |        |
------------------------------------------------------------
|        |          |         ||   D     |    E    |   F    |
------------------------------------------------------------
|        |          |         ||   G     |    H    |   I    |
------------------------------------------------------------
|   J    |    K     |   L     ||         |         |        |
------------------------------------------------------------

我希望这样显示:

预期结果

------------------------------------------------------------
|   A    |     B    |   C     ||   D     |    E    |   F    |
------------------------------------------------------------
|   J    |     K    |    L    ||   G     |    H    |   I    |
------------------------------------------------------------
|        |          |         ||         |         |        |
------------------------------------------------------------

希望我很清楚。

【问题讨论】:

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


【解决方案1】:

尝试下面的函数,稍作修改以传递 Mogsdad 于 2014 年 11 月 27 日为新表格提供的解决方案中的列,以响应线程 Faster way to find the first empty row

// Don's array approach - checks first column only
// With added stopping condition & correct result.
// From answer https://stackoverflow.com/a/9102463/1677912
// Added the passing of myColumn which needs to be a column range
// example use: var emptyRow = getFirstEmptyRowByColumnArray('B:B');
function getFirstEmptyRowByColumnArray(myColumn) {
  var spr = SpreadsheetApp.getActiveSpreadsheet();
  var column = spr.getRange(myColumn);
  var values = column.getValues(); // get all data in one call
  var ct = 0;
  while ( values[ct] && values[ct][0] != "" ) {
    ct++;
  }
  return (ct+1);
}

当然,如果您愿意,也可以改为只传递列并创建范围。

【讨论】:

    猜你喜欢
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    相关资源
    最近更新 更多