如何从 Google 文档中删除定位的图像?
How to erase positioned images from Google Documents?
我正在尝试创建一个函数来擦除 Google 文档中的所有内容。
到目前为止,我的代码如下所示,去掉了除 positioned images 之外的所有内容。
// clears document
function eraseContent(){
var body = DocumentApp.getActiveDocument().getBody();
body.clear();
// Remove all images in the document body.
var imgs = body.getImages();
for (var i = 0; i < imgs.length; i++) {
imgs[i].removeFromParent();
}
}
大部分代码来自here。
如何从我的文档中删除定位的图像?
遗憾的是,目前还没有删除ClassPositionedImage中定位图片的方法。但是当使用Google Docs API时,可以删除定位的图片。那么这个修改怎么样呢?修改后的脚本流程如下
- 检索段落。
- 检索定位图像的对象 ID。
- 使用检索到的对象 ID 在 Google 文档 API 中为 batchUpdate 方法创建请求主体。
- 删除定位的图片
为了使用示例脚本,在您 运行 脚本之前,请在高级 Google 服务和 API 中启用 Google 文档 API控制台如下
在高级 Google 服务中启用 Google 文档 API
- 在脚本编辑器上
- 资源 -> 高级 Google 服务
- 打开 Google 文档 API v1
在API控制台
启用Google文档API
- 在脚本编辑器上
- 资源 -> 云平台项目
- 查看API控制台
- 在“开始”中,单击 "Explore and enable APIs"。
- 在左侧,单击“库”。
- 在"Search for APIs & services"处输入"Docs"。然后点击 "Google Docs API".
- 单击“启用”按钮。
- 如果API已经启用,请不要关闭。
修改后的脚本:
请将 eraseContent()
替换为 运行
function eraseContent(){
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
body.clear();
// Retrieve paragraphs.
var paragraphs = body.getParagraphs();
// Retrieve the object IDs of the positioned images.
// Create request body for the method of batchUpdate in Google Docs API using the retrieved object IDs.
var requests = paragraphs.reduce(function(ar, e) {
return ar.concat(e.getPositionedImages().map(function(f) {
return {deletePositionedObject: {objectId: f.getId()}}
}));
}, []);
// Delete the positioned images.
if (requests.length > 0) {
Docs.Documents.batchUpdate({requests: requests}, doc.getId());
}
}
参考文献:
如果我误解了你的问题,我深表歉意。
我正在尝试创建一个函数来擦除 Google 文档中的所有内容。
到目前为止,我的代码如下所示,去掉了除 positioned images 之外的所有内容。
// clears document
function eraseContent(){
var body = DocumentApp.getActiveDocument().getBody();
body.clear();
// Remove all images in the document body.
var imgs = body.getImages();
for (var i = 0; i < imgs.length; i++) {
imgs[i].removeFromParent();
}
}
大部分代码来自here。
如何从我的文档中删除定位的图像?
遗憾的是,目前还没有删除ClassPositionedImage中定位图片的方法。但是当使用Google Docs API时,可以删除定位的图片。那么这个修改怎么样呢?修改后的脚本流程如下
- 检索段落。
- 检索定位图像的对象 ID。
- 使用检索到的对象 ID 在 Google 文档 API 中为 batchUpdate 方法创建请求主体。
- 删除定位的图片
为了使用示例脚本,在您 运行 脚本之前,请在高级 Google 服务和 API 中启用 Google 文档 API控制台如下
在高级 Google 服务中启用 Google 文档 API
- 在脚本编辑器上
- 资源 -> 高级 Google 服务
- 打开 Google 文档 API v1
在API控制台
启用Google文档API- 在脚本编辑器上
- 资源 -> 云平台项目
- 查看API控制台
- 在“开始”中,单击 "Explore and enable APIs"。
- 在左侧,单击“库”。
- 在"Search for APIs & services"处输入"Docs"。然后点击 "Google Docs API".
- 单击“启用”按钮。
- 如果API已经启用,请不要关闭。
修改后的脚本:
请将 eraseContent()
替换为 运行
function eraseContent(){
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
body.clear();
// Retrieve paragraphs.
var paragraphs = body.getParagraphs();
// Retrieve the object IDs of the positioned images.
// Create request body for the method of batchUpdate in Google Docs API using the retrieved object IDs.
var requests = paragraphs.reduce(function(ar, e) {
return ar.concat(e.getPositionedImages().map(function(f) {
return {deletePositionedObject: {objectId: f.getId()}}
}));
}, []);
// Delete the positioned images.
if (requests.length > 0) {
Docs.Documents.batchUpdate({requests: requests}, doc.getId());
}
}
参考文献:
如果我误解了你的问题,我深表歉意。