简体   繁体   English

Google-apps脚本:删除带有getValues中字符串的行

[英]Google-apps script: Remove rows with strings in getValues

I use a function which check the length of a column and insert unique-id's in each cell. 我使用一个函数来检查列的长度,并在每个单元格中插入唯一ID。

var sh = SpreadsheetApp.getActiveSheet();
var dataincol = sh.getRange(1,8,sh.getMaxRows(),1).getValues(); //getRange row, column, numRows, numColumns
//thanks to Serge insas

My problem is that the getValues() also takes strings, what I dont intend, and I dont know how to filter strings from my Range. 我的问题是getValues()也接受字符串,这是我不想要的,而且我也不知道如何从Range中过滤字符串。 The dataincol should take only free cells, ignoring strings and continue with the increase of the numbers. dataincol应该仅获取空闲单元,忽略字符串并继续增加数字。

Here you can see the full function: 在这里您可以看到全部功能:

function fillRowNumber(){
  var col = 1 ; // this for column A ! change to your needs
  var sh = SpreadsheetApp.getActiveSheet();
  var dataincol = sh.getRange(1,col,sh.getMaxRows(),1).getValues();
  var maxval =0
  for(n=0;n<dataincol.length;++n){
    if(Number(dataincol[n][0])>maxval){ maxval=Number(dataincol[n][0])}
    }
  for(n=1;n<sh.getLastRow();++n){
    if(dataincol[n-1][0] == ''){ dataincol[n-1][0]=maxval+1;++maxval}
    }
  sh.getRange(1,col,sh.getMaxRows(),1).setValues(dataincol);
}

Thank you for any help 感谢您的任何帮助

The two number validations I often use are: parseInt(val) == val which is a value check and val.toString().match(/[0-9\\-\\.]/i) > -1) which a string check. 我经常使用的两个数字验证是​​: parseInt(val) == val ,它是一个值检查; val.toString().match(/[0-9\\-\\.]/i) > -1) ,它是一个字符串校验。

In the code below, instead of double for{} loops I have used array functions. 在下面的代码中,我使用了数组函数来代替double for {}循环。 For loops works just as well, I just prefer the array functions in EC5. 对于循环也同样有效,我只喜欢EC5中的数组函数。

First builds an array just of numerical values in the column (with indexes preserved in case you want to use this for anything else down the line - alternative is to filter out non-numeric values) 首先,在该列中构建一个仅包含数值的数组(保留索引,以防您想将其用于行下的其他任何操作-替代方法是过滤掉非数值)

Second amends dataincol to fill in blanks with incremented maxval. 第二个修改dataincol以增加maxval来填充空白。

function fillRowNumber(){
  var col = 1;
  var sh = SpreadsheetApp.getActiveSheet();
  var dataincol = sh.getRange(1, col, sh.getMaxRows(), 1).getValues();
  var maxval = 0;

  var map = dataincol.map(function(row) { 
    // fill an array with numbers where numbers exist in data otherwise 0
    return (parseInt(row[0],10) == row[0]) ? row[0] * (row[0].toString().match(/[0-9\-\.]/i) > -1) : 0;
  });

  maxval = Math.max.apply(Math, map); // Mathmatical maximum

  dataincol = dataincol.map(function(row, index) { 
    if (row[0] == '') {
      maxval += 1; // increment maximum value
      // if blank cell then add new maximum value
      return (row[0] == '') ? [maxval] : [row[0]]; // if you add [&& index < sh.getLastRow()] to the conditional it restricts it to the original range of used cells not the maximum
    } else {
      // leave cell intact
      return [row[0]];
    }
  });

  sh.getRange(1, col, sh.getMaxRows(), 1).setValues(dataincol);
}

I wasn't clear if you wanted to keep adding maximum values for the whole sheet or just within the bounds of originally entered data. 我不清楚您是要继续为整个工作表添加最大值还是要在原始输入数据的范围内添加最大值。 Try the alternative I note in the code comment line 18 尝试在代码注释line 18记下的替代方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM