Last active
June 12, 2018 21:51
-
-
Save mogsdad/600bbb24ecb360da7d8c to your computer and use it in GitHub Desktop.
Google Apps Script function to retrieve a list of all PositionedImages in a document. See https://mogsdad.wordpress.com.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Get a list of all PositionedImages in a document. | |
* From: gist.github.com/mogsdad/600bbb24ecb360da7d8c | |
* See: stackoverflow.com/a/20661113/1677912 | |
* | |
* @param {String} docId (optional) ID of document to scan | |
* | |
* @returns {PositionedImage[]} Array of PositionedImages in document | |
*/ | |
function getAllPositionedImages( docId ) { | |
// Open document if given ID, otherwise use active document. | |
if (docId) { | |
var doc = DocumentApp.openById(docId); | |
} | |
else { | |
doc = DocumentApp.getActiveDocument(); | |
} | |
// Get handle on document's body | |
var body = doc.getBody(); | |
// array to hold all images in document | |
var allPositionedImages = []; | |
var numElems = body.getNumChildren(); | |
for (var childIndex=0; childIndex<numElems; childIndex++) { | |
var child = body.getChild(childIndex); | |
switch ( child.getType() ) { | |
case DocumentApp.ElementType.PARAGRAPH: | |
var container = child.asParagraph(); | |
break; | |
case DocumentApp.ElementType.LIST_ITEM: | |
container = child.asListItem(); | |
break; | |
default: | |
// Skip elements that can't contain PositionedImages. | |
continue; | |
} | |
// Collect images from current container | |
var imagesHere = container.getPositionedImages(); | |
allPositionedImages = allPositionedImages.concat(imagesHere); | |
} | |
return allPositionedImages; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment