简体   繁体   English

使用 Google Apps 脚本从 Google 电子表格中选择特定列中的值不为空的行

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

I have a dataset and here is the link for that:我有一个数据集,这是它的链接:

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

So what I want is,if any cell in column A has a date (not empty), I want to fetch the entire row.所以我想要的是,如果 A 列中的任何单元格有日期(非空),我想获取整行。

I even got one script in stack overflow which is:我什至在堆栈溢出中得到了一个脚本,它是:

  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
          }
        }

but if i use this to the dataset that I have shared I am getting this error "TypeError: Cannot set property "0.0" of undefined to "invited". (line 12, file "Code")".但是,如果我将此用于共享的数据集,则会收到此错误“TypeError:无法将未定义的属性“0.0”设置为“已邀请”。(第 12 行,文件“代码”)”。

Thanks in advance.提前致谢。

How about following script?下面的脚本怎么样? This script retrieves only rows that column A has strings.此脚本仅检索 A 列包含字符串的行。

Although you say that you want to retrieve rows by a condition of column B, your script watches column A as the condition.尽管您说要按 B 列的条件检索行,但您的脚本将 A 列视为条件。 So if you use this sample script, please change "col".因此,如果您使用此示例脚本,请更改“col”。 Now "col" is column A.现在“col”是A列。

Script:脚本:

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);
}

Result: This is imported to sheet2.结果:这将导入到 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.

相关问题 使用Google Apps脚本获取工作表中包含数据的特定行为空的所有行 - get all rows in a sheet with data where a specific column is empty using Google Apps Scripts 使用Google Apps脚本自动从电子表格中删除数据或将数据插入到电子表格中 - Using Google Apps Scripts to automate deleting from and inserting data into a spreadsheet 如何使用 Google Apps 脚本从 JSON 创建数组并将值导入到 Google 表格中的多行 - How to Create an Array From JSON and Import the Values to Multiple Rows in a Google Sheet Using Google Apps Scripts 如果列A为空,则隐藏Google电子表格中的行? - Hide rows in google spreadsheet if Column A is empty? 在Google Apps脚本中使用BigQuery连接到Google Spreadsheet - Using BigQuery in Google Apps Scripts to connect to Google Spreadsheet 修改代码 - 将某些行/列从一个电子表格复制到另一个 - Google Apps Script / Google Sheets - Modify Code - Copy Certain Rows/Columns From one Spreadsheet to Another - Google Apps Script / Google Sheets 如何使用Google Apps脚本将特定的行从3个Google电子表格复制到另一个电子表格 - How to copy specific rows from 3 google spreadsheet to another spreadsheet using google apps script 使用Google脚本将电子表格中的值存储在对象中 - storing values from a spreadsheet in an object with google scripts 通过 Google Apps Scripts #2 发布 Google 电子表格 - Publish a Google Spreadsheet through Google Apps Scripts #2 通过 Google Apps Scripts 发布 Google 电子表格 - publish a Google Spreadsheet through Google Apps Scripts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM