Reputation: 198
Is there a trick to using getPageElementType?
If you use this function you get "Logging output too large. Truncating output. pe type" followed by a meaningless dump of something. Is the only way to determine page element type to use the select / case structure and go through the entire enum?
Here is my code:
function minimalCode() {
const presentation = SlidesApp.getActivePresentation();
const slides = presentation.getSlides();
slides.forEach(slide => {
slide.getPageElements().forEach(pe => {
if (pe.getPageElementType() === 'SHAPE')
console.log('pe type: SHAPE' );
else
console.log('keep quessing the pe type - it is not a SHAPE' );
console.log('- - - - - - - - - - - ' );
console.log('pe type: ', pe.getPageElementType() )
return;
} ); // for each pe
return;
} ); // for each slide
}
Here is the result:
Mar 10, 2021, 2:35:47 PM Debug keep quessing the pe type - it is not a SHAPE
Mar 10, 2021, 2:35:47 PM Debug - - - - - - - - - - -
Mar 10, 2021, 2:35:47 PM Debug Logging output too large. Truncating output. pe type: { toString: [Function: toString],
name: [Function: toString],
toJSON: [Function: toString],
ordinal: [Function: ordinal],
compareTo: [Function: compareTo],
UNSUPPORTED_PAGE_ELEMENT:
{ toString: [Function: toString],
name: [Function: toString],
toJSON: [Function: toString],
ordinal: [Function: ordinal],
compareTo: [Function: compareTo],
UNSUPPORTED_PAGE_ELEMENT: [Circular],
UNSUPPORTED:
{ toString: [Function: toString],
name: [Function: toString],
toJSON: [Function: toString],
ordinal: [Function: ordinal],
compareTo: [Function: compareTo],
UNSUPPORTED_PAGE_ELEMENT: [Circular],
UNSUPPORTED: [Circular],
SHAPE: [Circular],
IMAGE: [Object],
VIDEO: [Object],
TABLE: [Object],
GROUP: [Object],
LINE: [Object],
WORD_ART: [Object],
SHEETS_CHART: [Object],
AUDIO: [Object] },
SHAPE: [Circular],
IMAGE:
{ toString: [Function: toString],
name: [Function: toString],
toJSON: [Function: toString],
ordinal: [Function: ordinal],
compareTo: [Function: compareTo],
UNSUPPORTED_PAGE_ELEMENT: [Circular],
UNSUPPORTED: [Object],
SHAPE: [Circular],
IMAGE: [Circular],
VIDEO: [Object],
TABLE: [Object],
GROUP: [Object],
LINE: [Object],
WORD_ART: [Object],
SHEETS_CHART: [Object],
AUDIO: [Object] },
VIDEO:
{ toString: [Function: toString],
name: [Function: toString],
toJSON: [Function: toString],
ordinal: [Function: ordinal],
compareTo: [Function: compareTo],
UNSUPPORTED_PAGE_ELEMENT: [Circular],
UNSUPPORTED: [Object],
SHAPE: [Circular],
IMAGE: [Object],
VIDEO: [Circular],
TABLE: [Object],
GROUP: [Object],
LINE: [Object],
WORD_ART: [Object],
SHEETS_CHART: [Object],
AUDIO: [Object] },
TABLE:
{ toString: [Function: toString],
name: [Function: toString],
toJSON: [Function: toString],
ordinal: [Function: ordinal],
compareTo: [Function: compareTo],
UNSUPPORTED_PAGE_ELEMENT: [Circular],
UNSUPPORTED: [Object],
SHAPE: [Circular],
IMAGE: [Object],
VIDEO: [Object],
TABLE: [Circular],
GROUP: [Object],
LINE: [Object],
WORD_ART: [Object],
SHEETS_CHART: [Object],
AUDIO: [Object] },
GROUP:
{ toString: [Function: toString],
name: [Function: toString],
toJSON: [Function: toString],
ordinal: [Function: ordinal],
compareTo: [Function: compareTo],
UNSUPPORTED_PAGE_ELEMENT: [Circular],
UNSUPPORTED: [Object],
SHAPE: [Circular],
IMAGE: [Object],
VIDEO: [Object],
TABLE: [Object],
GROUP: [Circular],
LINE: [Object],
WORD_ART: [Object],
SHEETS_CHART: [Object],
AUDIO: [Object] },
LINE:
{ toString: [Function: toString],
name: [Function: toString],
toJSON: [Function: toString],
ordinal: [Function: ordinal],
compareTo: [Function: compareTo],
UNSUPPORTED_PAGE_ELEMENT: [Circular],
UNSUPPORTED: [Object],
SHAPE: [Circular],
IMAGE: [Object],
VIDEO: [Object],
TABLE: [Object],
GROUP: [Object],
LINE: [Circular],
WORD_ART: [Object],
SHEETS_CHART: [Object],
AUDIO: [Object] },
WORD_ART:
{ toString: [Function: toString],
name: [Function: toString],
toJSON: [Function: toString],
ordinal: [Function: ordinal],
compareTo: [Function: compareTo],
UNSUPPORTED_PAGE_ELEMENT: [Circular],
UNSUPPORTED: [Object],
SHAPE: [Circular],
IMAGE: [Object],
VIDEO: [Object],
TABLE: [Object],
GROUP: [Object],
LINE: [Object],
WORD_ART: [Circular],
SHEETS_CHART: [Object],
AUDIO: [Object] },
SHEETS_CHART:
{ toString: [Function: toString],
name: [Function: toString],
toJSON: [Function: toString],
ordinal: [Function: ordinal],
compareTo: [Function: compareTo],
UNSUPPORTED_PAGE_ELEMENT: [Circular],
UNSUPPORTED: [Object],
SHAPE: [Circular],
IMAGE: [Object],
VIDEO: [Object],
TABLE: [Object],
GROUP: [Object],
LINE: [Object],
WORD_ART: [Object],
SHEETS_CHART: [Circular],
AUDIO: [Object] },
AUDIO:
{ toString: [Function: toString],
name: [Function: toString],
toJSON: [Function: toString],
ordinal: [Function: ordinal],
compareTo: [Function: compareTo],
UNSUPPORTED_PAGE_ELEMENT: [Circular],
UNSUPPORTED: [Object],
SHAPE: [Circular],
IMAGE: [Object],
VIDEO: [Object],
TABLE: [Object],
GROUP: [Object],
LINE: [Object],
WORD_ART: [Object],
SHEETS_CHART: [Object],
AUDIO: [Circular] } },
UNSUPPORTED:
{ toString: [Function: toString],
name: [Function: toString],
toJSON: [Function: toString],
ordinal: [Function: ordinal],
compareTo: [Function: compareTo],
UNSUPPORTED_PAGE_ELEMENT:
{ toString: [Function: toString],
name: [Function: toString],
toJSON: [Function: toString],
ordinal: [Function: ordinal],
compareTo: [Function: compareTo],
UNSUPPORTED_PAGE_ELEMENT: [Circular],
UNSUPPORTED: [Circular],
SHAPE: [Circular],
IMAGE: [Object],
VIDEO: [Object],
TABLE: [Object],
GROUP: [Object],
LINE: [Object],
WORD_ART: [Object],
SHEETS_CHART: [Object],
AUDIO: [Object] },
UNSUPPORTED: [Circular],
SHAPE: [Circular],
IMAGE:
{ toString: [Function: toString],
name: [Function: toString],
toJSON: [Function: toString],
ordinal: [Function: ordinal],
compareTo: [Function: compareTo],
UNSUPPORTED_PAGE_ELEMENT: [Object],
UNSUPPORTED: [Circular],
SHAPE: [Circular],
IMAGE: [Circular],
VIDEO: [Object],
TABLE: [Object],
GROUP: [Object],
LINE: [Object],
WORD_ART: [Object],
SHEETS_CHART: [Object],
AUDIO: [Object] },
VIDEO:
{ toString: [Function: toString],
name: [Function: toString],
toJSON: [Function: toString],
ordinal: [Function: ordinal],
compareTo: [Function: compareTo],
UNSUPPORTED_PAGE_ELEMENT: [Object],
UNSUPPORTED: [Circular],
SHAPE: [Circular],
IMAGE: [Object],
VIDEO: [Circular],
TABLE: [Object],
GROUP: [Object],
LINE: [Object],
WORD_ART: [Object],
SHEETS_CHART: [Object],
AUDIO: [Object] },
TABLE:
{ toString: [Function: toString],
name: [Function: toString],
toJSON: [Function: toString],
ordinal: [Function: ordinal],
compareTo: [Function: compareTo],
UNSUPPORTED_PAGE_ELEMENT: [Object],
UNSUPPORTED: [Circular],
SHAPE: [Circular],
IMAGE: [Object],
VIDEO: [Object],
TABLE: [Circular],
GROUP: [Object],
LINE: [Object],
WORD_ART: [Object],
SHEETS_CHART: [Object],
AUDIO: [Object] },
GROUP:
{ toString: [Function: toString],
name: [Function: toString],
toJSON: [Function: toString],
ordinal: [Function: ordinal],
compareTo: [Function: compareTo],
UNSUPPORTED_PAGE_ELEMENT: [Object],
UNSUPPORTED: [Circular],
SHAPE: [Circular],
IMAGE: [Object],
VIDEO: [Object],
TABLE: [Object],
GROUP: [Circular],
LINE: [Object],
WORD_ART: [Object],
SHEETS_CHART: [Object],
AUDIO: [Object] },
LINE:
{ toString: [Function: toString],
name: [Function:
Upvotes: 2
Views: 519
Reputation: 8974
Yup, there is a trick to it. When you invoke getPageElementType()
it returns an enumerated type. That enumerated type's value is different depending on the context in which its used.
When you used it to test equality via getPageElementType() === "SHAPE"
, its evaluated as a string, and behind the scenes the toString()
method is implicitly called on the enumerated type returned by getPageElementType()
, which returns the string "SHAPE"
, so the equality test evaluates as TRUE.
When you pass that enumerated type to console.log()
, the enumerated type is evaluated as an object and all its enumerable properties are printed out instead.
If you want to print out the string representation from console.log()
you have to explicitly call the toString()
method on the enumerated type (and this holds true for all enumerated types in Apps Script).
So, in short, do this:
function minimalCode() {
const presentation = SlidesApp.getActivePresentation();
const slides = presentation.getSlides();
slides.forEach(slide => {
slide.getPageElements().forEach(pe => {
if (pe.getPageElementType() === 'SHAPE')
console.log('pe type: SHAPE' );
else
console.log('keep quessing the pe type - it is not a SHAPE' );
console.log('- - - - - - - - - - - ' );
console.log('pe type: ', pe.getPageElementType().toString());
return;
} ); // for each pe
return;
} ); // for each slide
}
Upvotes: 1
Reputation: 5963
Console.log() outputs a blank DEBUG level message to Stackdriver Logging. You can use Logger.log(data) instead.
function minimalCode() {
const presentation = SlidesApp.getActivePresentation();
const slides = presentation.getSlides();
slides.forEach(slide => {
slide.getPageElements().forEach(pe => {
if (pe.getPageElementType() === "SHAPE")
Logger.log('pe type: SHAPE' );
else
Logger.log('keep quessing the pe type - it is not a SHAPE' );
Logger.log('- - - - - - - - - - - ' );
Logger.log('pe type: '+ pe.getPageElementType() )
return;
} ); // for each pe
return;
} ); // for each slide
}
6:21:38 AM Notice Execution started
6:21:40 AM Info keep quessing the pe type - it is not a SHAPE
6:21:40 AM Info - - - - - - - - - - -
6:21:40 AM Info pe type: SHAPE
6:21:40 AM Info keep quessing the pe type - it is not a SHAPE
6:21:40 AM Info - - - - - - - - - - -
6:21:40 AM Info pe type: IMAGE
6:21:40 AM Info keep quessing the pe type - it is not a SHAPE
6:21:40 AM Info - - - - - - - - - - -
6:21:40 AM Info pe type: TABLE
6:21:40 AM Notice
SHAPE
, the if condition was not triggered. To fix this use if (pe.getPageElementType() == "SHAPE")
Output:
6:27:30 AM Notice Execution started
6:27:31 AM Info pe type: SHAPE
6:27:31 AM Info - - - - - - - - - - -
6:27:31 AM Info pe type: SHAPE
6:27:31 AM Info keep quessing the pe type - it is not a SHAPE
6:27:31 AM Info - - - - - - - - - - -
6:27:31 AM Info pe type: IMAGE
6:27:31 AM Info keep quessing the pe type - it is not a SHAPE
6:27:31 AM Info - - - - - - - - - - -
6:27:31 AM Info pe type: TABLE
6:27:31 AM Notice Execution completed
Upvotes: 0