Martyn Bowis
Martyn Bowis

Reputation: 41

Error: Could not locate target object while calling method getPageElementRange on object with id 45

A function that was working is now showing the following error:

Logger: error=Error: Could not locate target object while calling method getPageElementRange on object with id 45.

Wondering if this is an intermittent bug that has arisen in a recent release of app script as my code has not changed?

Code that does not work is:

var presentation = SlidesApp.getActivePresentation();
var slide = presentation.getSelection().getCurrentPage().asSlide();
var arrPageElementsSelected = presentation.getSelection().getPageElementRange().getPageElements();

If I break this last line down into three lines, it still does not work:

var presentation = SlidesApp.getActivePresentation();
var slide = presentation.getSelection().getCurrentPage().asSlide();
var selection = presentation.getSelection();
var rangePageElements = selection.getPageElementRange();
var arrPageElementsSelected = rangePageElements.getPageElements();

Again, this code used to work up until the last few days. I am thinking that something has become broken in the Google App Script engine.

Upvotes: 3

Views: 1021

Answers (2)

dekuShrub
dekuShrub

Reputation: 486

I think presentation isn't referring to an existing presentation anymore. I believe it's because you're storing the result of getActivePresentation() in presentation, and that the reference is removed by the app or something like that. It might be a kind of garbage collection that removes variables based on time since creation or total memory used.

I encountered the same error in google sheets where I tried to manage several sheets from apps script and I had defined a global variable to the function in a second script like this:

var getSheetByName = SpreadsheetApp.getActive().getSheetByName;

Then I boiled down the problem to this:

function target_error_test()
{
  var getSheetByName = SpreadsheetApp.getActive().getSheetByName;

  var i = 0
  for (var i = 0; true; i++)
  {
    Logger.log(`try nr ${i}...`)
    var input_sheet = getSheetByName("sheet2")
  }
}

which produced this output (still with the same error)

enter image description here

So it executes without fail 9 times and then sudden crash - probably due to garbage collection or something like that.

Solution

Only use these variables for a short amount of time. One can store global variables with

PropertiesService.getScriptProperties().setProperty(key, value)
PropertiesService.getScriptProperties().getProperty(key)

And functions are not removed, so it's better to use a function in my case:

function getSheetByName(sheetName)
{
  return SpreadsheetApp.getActive().getSheetByName(sheetName);
}

Upvotes: 1

Martyn Bowis
Martyn Bowis

Reputation: 41

I think I have now solved this, with the following code working:

var presentation = SlidesApp.getActivePresentation();    
var arrPageElementsSelected = presentation.getSelection().getPageElementRange().getPageElements();

var slide = presentation.getSelection().getCurrentPage().asSlide();
var arrPageElementsAll = slide.getPageElements();

It would appear that the issue was due to my earlier code defining presentation and slide then calling getPageElementRange() afterwards.

The solution was to first define presentation and call getPageElementRange() before then defining slide and using asSlide() to do that.

The issue I think is to do with asSlide() used to define slide dropping the reference to presentation, which was needed to call getPageElementRange().

Upvotes: 0

Related Questions