批量删除Excel中的图片.md

摘要

在Excel中删除内容时, 我们可以通过Delete或者是BackSpace直接删除。如果想批量删除, 可以通过Ctrl+A全选后再删除。但是有一种特殊情况,当Excel工作表中存在图片、图表、形状时,并不能通过Ctrl+A来实现全选。可以通过自动化实现批量删除。

手动批量删除图片

图片是Excel中的特殊对象,我们可以通过Go To Special来实现选中所有的图片。

NOTE: Go To Special会选中所有对象,如图片、图表、形状等。如果是要清除所有,可以使用Go To Special,否则手动方式建议Ctrl或者Shift依次选中要删除的对象。

具体步骤

  • Home Tab -> Editing Tab -> Find & Select -> Go To Special
  • 选中Objects

GoToSpecial

  • 点击Delete或者是BackSpace

VBA批量删除图片

1
2
3
4
5
6
7
8
9
10
Sub BatchDeleteImages()
Dim meekouWorkSheet As Worksheet
Dim meekouImage As Picture
For Each meekouWorkSheet In ActiveWorkbook.Worksheets
For Each meekouImage In meekouWorkSheet.Pictures
' Debug.Print meekouImage
meekouImage.Delete
Next
Next
End Sub

Script Lab批量删除图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
name: 批量删除工作表中的图片
description: Create a new snippet from a blank template.
host: EXCEL
api_set: {}
script:
content: |
$("#run").click(() => tryCatch(run));

async function run() {
await Excel.run(async (context) => {
const sheets = context.workbook.worksheets;
sheets.load("items/shapes/items/type");
await context.sync();
sheets.items.forEach(sheet =>{
let shapes = sheet.shapes;
shapes.items.forEach(shape => {
console.log(shape.type);
if (shape.type == Excel.ShapeType.image) {
shape.delete();
}
})
})
await context.sync();
});
}

/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
try {
await callback();
} catch (error) {
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
console.error(error);
}
}
language: typescript
template:
content: |
<button id="run" class="ms-Button">
<span class="ms-Button-label">批量删除工作表中的图片</span>
</button>
language: html
style:
content: |-
section.samples {
margin-top: 20px;
}

section.samples .ms-Button, section.setup .ms-Button {
display: block;
margin-bottom: 5px;
margin-left: 20px;
min-width: 80px;
}
language: css
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js

office-ui-fabric-js@1.4.0/dist/css/fabric.min.css
office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css

core-js@2.4.1/client/core.min.js
@types/core-js

jquery@3.1.1
@types/jquery@3.3.1