我正在尝试创建一个功能,该功能将擦除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。
我该怎么做才能从文档中删除定位的图像?
答案 0 :(得分:1)
不幸的是,在当前阶段,还没有方法可以删除Class PositionedImage类中的定位图像。但是,当使用Google Docs API时,可以删除定位的图像。那么如何修改呢?修改后的脚本的流程如下。
要使用示例脚本,请在运行脚本之前先按以下步骤在高级Google服务和API控制台上启用Google Docs 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());
}
}
如果我误解了你的问题,我表示歉意。