SlidesApp, replaceAllText() --> Exception: This request cannot be applied.

415 views
Skip to first unread message

Mateusz Krajewski

unread,
Mar 7, 2024, 10:53:49 AM3/7/24
to Google Apps Script Community
Hi there,

I wrote a script that replaced the text on my Google Slides presentation. There are many many slides so I wrote a for loop. Every time I run the function I get this error: "
Exception: This request cannot be applied." 
It is very confusing as all the occurrences are being replaced... I have no idea what is wrong here.

This is the part of the code I use to replace the placeholders:

for (let i = 0; i < presentationCopy.getSlides()?.length; i++) {
      const currentSlide = presentationCopy.getSlides()[i];
      offerReplacements.forEach(replacement => {
        const searchText = replacement[0];
        const newText = replacement[1];
        currentSlide.replaceAllText(searchText, newText)
      })
    };


offerReplacements is an array, sth like this:

const offerReplacements = [
      ["{{xyz}}", text],
      ["{{zxy}", text2],
        ...etc
    ]


Anyone here who may know what causes this strange behavior?
Cheers,
Mat

Keith Andersen

unread,
Mar 8, 2024, 9:58:53 PM3/8/24
to google-apps-sc...@googlegroups.com

presentationCopy.getSlides()?.length; i++)

Why the ?

Try:
presentationCopy.getSlides().length; i++)


Passions: God, Family, Scriptures, Learning, Data Management, Google Sheets + App Script and much more!

--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/72774474-4952-4276-9c0c-7122f74e5be0n%40googlegroups.com.

Mateusz Krajewski

unread,
Mar 9, 2024, 5:16:07 AM3/9/24
to Google Apps Script Community
I am using "?" sign to check whether the array contains anything and handle the possible error. If presentationCopy.getSlides() is undefined, the loop won't be executed, that is all, and I do not think that is the case here. In my opinion, it is related to API quota limits but I am not sure... My presentation contains more than 20 pages and when I limited the loop to work only through the first 7 slides it worked without any errors (I also tried Utilities.sleep(). Still, it slowed down the execution significantly). Even though, thanks for your reply Keith, appreciate that!

DME

unread,
Mar 14, 2024, 4:56:01 AM3/14/24
to google-apps-sc...@googlegroups.com
1. Placeholders Not Present on All Slides:

The offerReplacements array might contain placeholders ({{xyz}}, {{zxy}}) that are not present on every slide in your presentation. The replaceAllText method tries to replace all occurrences, even if the search text isn't found, leading to the error.

Solution:

Before using replaceAllText, check if the current slide contains the search text. You can use the getText method of the slide object to get the slide's content as a string and search for the placeholder using includes or indexOf:

JavaScript

const currentSlideText = currentSlide.getText();
if (currentSlideText.includes(searchText)) {
  currentSlide.replaceAllText(searchText, newText);
}

Use code with caution.
content_copy

2. Concurrent Modifications (Less Likely):

In rare cases, if replaceAllText modifies the slide content within the loop, subsequent iterations might encounter issues due to changes made in previous iterations. This is less likely but worth considering.

Solution:

Create a copy of the current slide's text content, perform replacements on the copy, and then set the modified text back to the slide using setText:

JavaScript

const currentSlideText = currentSlide.getText();
let modifiedText = currentSlideText;
offerReplacements.forEach(replacement => {
  modifiedText = modifiedText.replaceAll(replacement[0], replacement[1]);
});
currentSlide.setText(modifiedText);


content_copy

Additional Considerations:

Error Handling: Consider adding error handling within the forEach loop to catch specific errors (try...catch) if the issue persists after implementing the above solutions.
Logging: If the scripts still encounters errors after implementing these suggestions, include logging within the loop to track which slides trigger the error and the placeholders involved. This can help narrow down the problem area.

By implementing these checks and modifications, you should be able to handle scenarios where placeholders aren't present on all slides and hopefully avoid the "This request cannot be applied" error.

Mateusz Krajewski

unread,
Mar 14, 2024, 6:42:48 AM3/14/24
to Google Apps Script Community
Good points! The first point might be the case then. There are some slides without placeholders. I didn't know that replaceText() would return an error in case of not finding the text to replace.
Thanks, DME. I will check that out 🙂
Reply all
Reply to author
Forward
0 new messages