簡體   English   中英

使用 Google Apps 腳本從 Google 電子表格中選擇特定列中的值不為空的行

[英]Select the rows from a Google Spreadsheet where values in a certain column are not empty using Google Apps Scripts

我有一個數據集,這是它的鏈接:

https://docs.google.com/spreadsheets/d/1QgR7WC2bj2_AW7yTDnjEXDrYKGo1GR_w_ea-8PtRwm4/edit?usp=sharing

所以我想要的是,如果 A 列中的任何單元格有日期(非空),我想獲取整行。

我什至在堆棧溢出中得到了一個腳本,它是:

  function copynotempty(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sh = SpreadsheetApp.setActiveSheet(ss.getSheets()[0])
  var col = 0 ; // choose the column you want to check: 0 = col A, 1= col B ...
  var range = sh.getDataRange();
  var values=range.getValues();// data is a 2D array, index0 = col A
  var formulas=range.getFormulas();// data is a 2D array, index0 = col A
  var target=new Array();// this is a new array to collect data
   for(n=0;n<range.getHeight();++n){
     if (values[n][col]!=''){ ; 
                for (cc=0;cc<range.getWidth();++cc){
                if (values[n][cc]!=''){target[n][cc]=values[n][cc]}
    // if the cell has a formula copy it or else copy the value, do that for the whole row
// (this also defines and apply the 'priority' you mentioned in your question, I wasn't sure if it should apply to the whole row or only on column B)
                }
              }
            }
            if(target.length>0){// if there is something to copy
          var sh2=SpreadsheetApp.setActiveSheet(ss.getSheets()[1]); //second sheet of your spreadsheet
          sh2.getRange(1,1,target.length,target[0].length).setValues();// paste the selected values in the 2cond sheet
          }
        }

但是,如果我將此用於共享的數據集,則會收到此錯誤“TypeError:無法將未定義的屬性“0.0”設置為“已邀請”。(第 12 行,文件“代碼”)”。

提前致謝。

下面的腳本怎么樣? 此腳本僅檢索 A 列包含字符串的行。

盡管您說要按 B 列的條件檢索行,但您的腳本將 A 列視為條件。 因此,如果您使用此示例腳本,請更改“col”。 現在“col”是A列。

腳本:

function copynotempty() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var range = ss.getSheets()[0].getDataRange();
  range.setNumberFormat('@');
  var values = range.getValues();
  col = 0;
  var target = values.filter(function(e, i){return (isNaN(e[col]) && i > 0)});
  ss.getSheets()[1].getRange(1,1,target.length,target[0].length).setValues(target);
}

結果:這將導入到 sheet2。

[[22/1/16, 2/2/16, 3/2/16, 3/2/16, 6/2/16],
[13/1/16, 13/1/16, 13/1/16, 13/1/16, 20/1/16],
[2/2/16, 2/2/16, 2/2/16, 2/2/16, 9/2/16],
[1/2/16, 1/2/16, 8/3/16, 14/3/16, 2/5/16],
[11/1/16, 11/1/16, 11/1/16, , ],
[12/2/16, 12/2/16, 12/2/16, 12/2/16, 20/2/16],
[28/1/16, 28/1/16, 28/1/16, , ],
[6/1/16, 6/1/16, 6/1/16, 6/1/16, 15/1/16],
[25/1/16, 25/1/16, 25/1/16, 25/1/16, 13/2/16],
[30/1/16, 3/2/16, 10/2/16, 10/2/16, 14/2/16],
[27/1/16, 27/1/16, 27/1/16, 27/1/16, 8/2/16],
[15/1/16, 23/1/16, 23/1/16, 23/1/16, 29/1/16],
[12/1/16, 12/1/16, 12/1/16, 12/1/16, 16/1/16],
[2/2/16, 3/2/16, 3/2/16, 3/2/16, 6/2/16],
[18/12/15, 5/1/16, 5/1/16, 5/1/16, 12/1/16]]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM