简体   繁体   English

如何使用 Google Sheets App 删除 A 列中不包含特定文本的行?

[英]How to delete rows that do not containing specific text within A column using Google Sheets App?

I'm new to Google Apps Scripts so I am sorry if my question is redundant.我是 Google Apps Scripts 的新手,所以如果我的问题是多余的,我很抱歉。

I am working on a variant of the scripts here:我正在这里处理脚本的变体:

Google Sheets: delete rows containing specified data Google 表格:删除包含指定数据的行

(Here is the script that I have been editing -- note the empty IF value.) (这是我一直在编辑的脚本——注意空的 IF 值。)

function onOpen(){
}
function deleteFunction(){
var sheetName = "Title"; 
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName(sheetName);

var dataRange = sheet.getDataRange();
var numRows = dataRange.getNumRows();
var values = dataRange.getValues();


var rowsDeleted = 0;
for (var i = 2; i <= numRows; i++){
var rowValues = values[i-1].toString();

if (rowValues == ""){
  sheet.deleteRow(i - rowsDeleted);
  rowsDeleted++; 
}

I would like to delete rows in a sheet named "Title" that do not contain "| My Text Here".我想删除名为“Title”的工作表中不包含“| My Text Here”的行。 The text is found in a string of text, such as "Here is the string of text that could be random | My Text Here".文本是在文本字符串中找到的,例如“这里是可能是随机的文本字符串| 我的文本在这里”。 So if "| My Text Here" is not found in a cell, I want that whole row to be deleted.因此,如果在单元格中找不到“| My Text Here”,我希望删除整行。

The data set I want this to work with will have ~10,000 rows and I want this script to either run when the sheet is opened or once a day.我希望它使用的数据集将有大约 10,000 行,我希望这个脚本在工作表打开时运行或每天运行一次。

I have tried to make this work, but I think I'm on the wrong track and so would really apprecaite the communities help!我已经尝试完成这项工作,但我认为我走错了路,所以真的很感谢社区的帮助!

I can attach a test sheet if needed.如果需要,我可以附上测试表。

Thank you in advance for your help and guidance预先感谢您的帮助和指导

New script:新脚本:

  function main() {

  var SS = SpreadsheetApp.getActive();
  var SHEET = SS.getSheetByName("Title");
  var RANGE = SHEET.getDataRange();


  var DELETE_VAL = "| TEST TEXT HERE";
  var COL_TO_SEARCH = 0; //Zero is first

  var deleteSelectedRows = removeThenSetNewVals();


  };

  function removeThenSetNewVals(){

  var SS = SpreadsheetApp.getActive();
  var SHEET = SS.getSheetByName("Title");
  var RANGE = SHEET.getDataRange();
  var rangeVals = RANGE.getValues();

  var DELETE_VAL = "| TEST TEXT HERE";
  var COL_TO_SEARCH = 0; //Zero is first

  var newRangeVals = [];

  for(var i = 0; i < rangeVals.length; i++){
  if(rangeVals[i][COL_TO_SEARCH] == DELETE_VAL){

  newRangeVals.push(rangeVals[i]);
   };
  };

  RANGE.clearContent();

  var newRange = 
  SHEET.getRange(1,1,newRangeVals.length, newRangeVals[0].length);
  newRange.setValues(newRangeVals);
   };
  }

Your code needs the following modification to work in the way you desire您的代码需要进行以下修改才能按您希望的方式工作

  1. Use indexOf() .使用indexOf() This is a Javascript method that allows you to verify either a sub-string is contained in a string.这是一种 Javascript 方法,可让您验证字符串中是否包含子字符串。 In your case, it allows you to verify either "| My Text Here" is contained in your cells in column A.在您的情况下,它允许您验证“| My Text Here”是否包含在 A 列的单元格中。

  2. Loop "backwards" from the end row to the start row.从结束行到开始行“向后”循环。 Why?为什么? Because otherwise the deletion of rows will mess up the row indices of the remaining rows.因为否则行的删除会弄乱剩余行的行索引。

function deleteFunction(){
  //declarations
  var sheetName = "Title"; 
  var ss = SpreadsheetApp.getActive();
  var sheet = ss.getSheetByName(sheetName);
  var dataRange = sheet.getDataRange();
  var values = dataRange.getValues();
  for(var i=values.length-1;i>0;i--){
    var myValue=values[i][0];
          Logger.log(i);
    if( myValue.indexOf("| My Text Here")==-1){
      Logger.log(myValue);
      sheet.deleteRow(i+1);
    }
  }
}

暂无
暂无

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

相关问题 Google表格:删除包含指定数据的行 - Google Sheets: delete rows containing specified data 如何从包含链接文本的单元格列中提取 url,其中包含 Google 表格中的数值? - How do you extract urls from a column of cells containing linked text with numeric values in Google Sheets? Google表格 - 如何找到包含特定文字的第一行编号,然后清除/删除所有要结束的内容? - Google Sheets - How can I find the first row number containing specific text, and then clear/delete all content to end? 在 Google 表格中删除包含特定文本的行 - Removing a row containing a specific text in Google Sheets 如何让 Google 脚本在特定行范围内的特定列中隐藏带有空白或零的行? - How do I get Google Script to hide rows with blanks or zeroes in a specific column, within a specific range of rows? 我如何更改此 google 应用程序脚本以根据列日期隐藏工作表列而不是行 - how do I change this google app script to hide sheets columns instead of rows based on column dates 使用 Apps 脚本从 Google 表格中的另一张表格中删除包含值的行 - Delete rows containing value from another sheet in Google Sheets using Apps Script 如何将特定数量的行导出到 Google 表格中的文本文件 - How to export a specific number of rows to text files in Google Sheets 如何使用 Google Script 从 Google 表格中提取特定列? - How do I extract a specific Column from google sheets using Google Script? 如何使用 Apps 脚本删除 Google 表格中的行? - How to delete rows in Google Sheets using Apps Script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM