Google表格脚本 - 在校正和删除行中查找值 [英] Google Sheet Script - Find Value in Col and Delete Row

查看:95
本文介绍了Google表格脚本 - 在校正和删除行中查找值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在编写一个脚本来搜索我的Google表格中的值,然后删除该行,到目前为止,我有这个:

  function sort(){
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 0;

for(var i = 0; i <= numRows - 1; i ++){
var row = values [i];
if(row [0] =='test'){
sheet.deleteRow((parseInt(i)+1) - rowsDeleted); rowsDeleted ++;
}
}
};

到目前为止,我可以完全匹配文本,但我想查找所有其中包含测试,而不仅仅是测试。因此,它会删除包含以下数据的行:


  1. 这是一项测试

  2. / li>
  3. ThisTestIsNotWorking

任何解决方案:)?

=h2_lin>解决方案

$ c
$ b

解决方案的问题在于,它只检查行是否等于 test ,而是检查该行是否包含 test


So I'm working on a script to search my Google Sheet for a value and then delete the row, so far I have this:

function sort() { 
  var sheet = SpreadsheetApp.getActiveSheet(); 
  var rows = sheet.getDataRange(); 
  var numRows = rows.getNumRows(); 
  var values = rows.getValues(); 
  var rowsDeleted = 0; 

  for (var i = 0; i <= numRows - 1; i++) { 
    var row = values[i]; 
    if (row[0] == 'test') { 
      sheet.deleteRow((parseInt(i)+1) - rowsDeleted); rowsDeleted++; 
    } 
  } 
}; 

So what I got so far is that I can exact match the text, but I want to find all cols which contains "test" and not only "test". So it will delete rows with data like:

  1. This is a test
  2. Test me again
  3. ThisTestIsNotWorking

Any solution :)?

解决方案

Use this instead: if (row[0].indexOf('test') > -1)

The problem with your solution was that it only checked if the row was exactly equal to test, this instead checks if the row contains test at all.

这篇关于Google表格脚本 - 在校正和删除行中查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆