Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 使用谷歌应用程序脚本,从谷歌电子表格中选择某些列中的值不为空的行_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script 使用谷歌应用程序脚本,从谷歌电子表格中选择某些列中的值不为空的行

Google apps script 使用谷歌应用程序脚本,从谷歌电子表格中选择某些列中的值不为空的行,google-apps-script,google-sheets,Google Apps Script,Google Sheets,我有一个数据集,以下是该数据集的链接: 所以我想要的是,如果列A中的任何单元格都有日期(不是空的),我想要获取整行 我甚至在堆栈溢出中得到了一个脚本,它是: function copynotempty(){ var ss = SpreadsheetApp.getActiveSpreadsheet(); var sh = SpreadsheetApp.setActiveSheet(ss.getSheets()[0]) var col = 0 ; // choose the col

我有一个数据集,以下是该数据集的链接:

所以我想要的是,如果列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
          }
        }
函数copynotempty(){
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sh=SpreadsheetApp.setActiveSheet(ss.getSheets()[0])
var col=0;//选择要检查的列:0=col A,1=col B。。。
var range=sh.getDataRange();
var values=range.getValues();//数据是一个2D数组,index0=col a
var formulas=range.getFormulas();//数据是一个2D数组,index0=col a
var target=new Array();//这是一个用于收集数据的新数组

对于(n=0;n以下脚本如何?此脚本仅检索列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);
}
结果:这将导入到图纸2中

[[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]]

非常感谢脚本。但是,例如,如果我在sheet1中有数据集,并且如果我试图通过在sheet2中写入“=CopyNoteEmpty(sheet1!a:E)”来使用脚本,该脚本为空,则会抛出一个错误“您没有调用setNumberFormat的权限(第4行)”你知道为什么会发生这种情况吗?更新了示例脚本。它将结果导入到sheet2。也谢谢你。你能按“接受”按钮回答这个问题吗?