[英]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);
};
}
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 列的单元格中。
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.