我正在尝试创建一个函数,该函数将擦除Google文档中的所有内容。
到目前为止,我的代码如下,除定位的图像外,所有内容均被删除。
// 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();
}
}
此代码大部分来自此处。
如何删除文档中的定位图像?
不幸的是,当前阶段,在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());
}
}
如果我误解了您的问题,我深表歉意。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句