跳到主要内容

如何删除Google表格中某列中包含特定文本的所有行?

假设您在Google工作表中有一系列数据,现在,您想根据列中的单元格值删除行。 例如,我想删除Colum C中所有包含“ Complete”文本的行。本文将讨论如何在Google表格中解决该问题。

使用脚本代码删除一列中包含特定文本的所有行


使用脚本代码删除一列中包含特定文本的所有行

要删除列中包含特定文本的所有行,请执行以下脚本代码,具体操作如下:

1。 点击 工具 > 脚本编辑器,请参见屏幕截图:

doc删除包含1的行

2。 在新打开的代码窗口中,将以下代码复制并粘贴到空白代码窗口中,请参见屏幕截图:

function deleteRows() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName('delete containing');
  var r = s.getRange('C:C');
  var v = r.getValues();
  for(var i=v.length-1;i>=0;i--)
    if(v[0,i]=='Complete')
      s.deleteRow(i+1);
};

doc删除包含2的行

备注:在上面的代码中,“删除包含“是包含您要删除的数据的工作表名称,”C:C”是您要从中删除特定文字的列,“完成:”是您要基于其删除行的特定文本,请根据需要更改它们。

3。 然后保存此代码,然后单击 运行 代码窗口中的按钮执行此脚本代码,请参见屏幕截图:

doc删除包含3的行

4。 并且包含特定文本“ Complete”的所有行都被立即删除,请参见屏幕截图:

doc删除包含4的行


删除Excel工作表中包含特定文本的所有行:

如果您要删除列中包含特定值的所有行,则 Kutools for Excel's 选择特定的单元格 功能可以帮助您选择符合条件的所有行,然后只需要立即删除这些行。

doc删除包含5的行

Kutools for Excel:具有300多个方便的Excel加载项,可以在30天内免费试用,没有任何限制。 立即下载并免费试用!

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (16)
Rated 5 out of 5 · 1 ratings
This comment was minimized by the moderator on the site
This formula work for all column A and check all cells that contain some text > 0 and delete only the rows that contain some text in cells > 0

function deleteRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('Sheet_name_here);
var r = s.getRange('A:A');
var v = r.getValues();
for(var i=v.length-1;i>=0;i--)
if(v[0,i]>'0')
s.deleteRow(i+2);
};
Rated 5 out of 5
This comment was minimized by the moderator on the site
Bonjour, j'aime beaucoup ce script. Moi je cherche un script qui permet de supprimer automatiquement tous les éléments ou contenus d'une colonne après chaque 1 min. Pas supprimer la colonne, mais le contenu de la colonne. Pouvez vous m'aider ?
This comment was minimized by the moderator on the site
How can I make this more versatile?

LEt's say that I create a list of blacklisted words in Sheet1 and I want to delete all the rows in Sheet2 that contain that words in column A. I'm totally stuck on this on Google Sheets
This comment was minimized by the moderator on the site
Any luck with this??
This comment was minimized by the moderator on the site
I fixed this for myself using the following.
The reason it is not working is because the v returns an array with only one item [0]
I also used Javascript .includes() so that you can delete records that "Contain" that text.


function deleteRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('default');
var r = s.getRange('A:A');
var v = r.getValues();
for(var i=v.length-1;i>=1;i--)
if(v[i][0].includes("appleid.com")) {
console.log('deleted row ' + v[i][0]);
s.deleteRow(i+1);
}
};
This comment was minimized by the moderator on the site
Thank you so much. Works great for me
This comment was minimized by the moderator on the site
Great job, working seamlessly.
This comment was minimized by the moderator on the site
Figured it out, but having trouble making it work for larger sheets since it's exceeding maximum runtime. Basically, the data that was being returned for 'v' was an 'object' type so you had to JSON.stringify() it first in order to detect values.
<div data-tag="code">function deleteRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('REPLACE WITH NAME OF SHEET');
var r = s.getRange('C:C'); // Replace 'C:C' to be the column you want to replace like start:end
var v = r.getValues();
for(var i=v.length-1;i>=0;i--) {
if(JSON.stringify(v[i]).includes('REPLACE WITH THE CHAR YOU WANT TO REPLACE')) {
s.deleteRow(i+1);
}
}
};
This comment was minimized by the moderator on the site
Why use the comma operator in v[0,i] ?
This comment was minimized by the moderator on the site
This seems to work only if a cell contains only the text specified. I want to delete rows with cells that contain other text including the text specific. I have a bunch of email addresses and want to be rid of rows containing Gmail addresses, therefore I'm looking for cells containing 'gmail.com'.

No luck here.
This comment was minimized by the moderator on the site
Replace this line: if(v[0,i]=='Complete')
With this: if(v[0,i].indexOf('gmail') > -1)
This comment was minimized by the moderator on the site
Doesn't work for me, either.
This comment was minimized by the moderator on the site
I tried that. It's still not working.
This comment was minimized by the moderator on the site
Hello,
Can modify the below code to delete the coloured row instead of text


function deleteRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('delete containing');
var r = s.getRange('C:C');
var v = r.getValues();
for(var i=v.length-1;i>=0;i--)
if(v[0,i]=='Complete')
s.deleteRow(i+1);
};
This comment was minimized by the moderator on the site
Hello,
Can I modify the below code to delete a coloured cell instead of specific text:


function deleteRows() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = ss.getSheetByName('delete containing');
var r = s.getRange('C:C');
var v = r.getValues();
for(var i=v.length-1;i>=0;i--)
if(v[0,i]=='Complete')
s.deleteRow(i+1);
};
This comment was minimized by the moderator on the site
Does nothing at all, not sure how to fix this.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations