Search code examples
google-apps-scriptgoogle-slides-api

How do I execute requests in Google slides apt script replaceAllText


I'm playing around with a dynamic signage project and need a little help finishing off some GAS in Slides. All I want to do is replace some text in the active presentation with some text from (one cell) in a Google spreadsheet. I think I have the Request syntax correct but not sure how to execute the Request on the current presentation as everything I've tried seems to fail. Any help would be much appreciated. Code below which includes the full script which copies some slides etc (and is working) - it's just the last request section that I need help with...

    function updateRBFixtures() {

    var srcPresentationId = "1zRvc0H64yvxxxxxxxxxxxxxznmb2usZ_4w6bvRA";
    var copysrcSlideIndex = 0; 

    var copydstSlideIndex = 1; 

    var src = SlidesApp.openById(srcPresentationId).getSlides()[copysrcSlideIndex];

    SlidesApp.getActivePresentation().insertSlide(copydstSlideIndex, src);


    var selection = SlidesApp.getActivePresentation().getSelection();
    var slide = SlidesApp.getActivePresentation().getSlides()[0];
    slide.remove();

    var dataRangeNotation = 'slideInfo!B2:B2';
    var value = 
    SpreadsheetApp.openById("1jiYxxxxxxxxxxxxxxMqtwXnc").getRange(dataRangeNotation).getValues();

    requests = [{
    replaceAllText: {
    containsText: {
    text: '{fixtureOneMatch}',
    matchCase: false
    },
   replaceText: value
   }
   }];




   }

Solution

  • How about this answer?

    Modification points:

    • getValues() returns 2 dimensional array. In your case, the value is retrieved from one cell. So you can use getValue().
    • In your case, after the slide was inserted, I think that it is required to use saveAndClose().
    • In your script, requests is not used.

    When above points are reflected to your script, it becomes as follows.

    Modified script:

    From:
     SlidesApp.getActivePresentation().insertSlide(copydstSlideIndex, src);
    
    
     var selection = SlidesApp.getActivePresentation().getSelection();
     var slide = SlidesApp.getActivePresentation().getSlides()[0];
     slide.remove();
    
     var dataRangeNotation = 'slideInfo!B2:B2';
     var value = 
     SpreadsheetApp.openById("1jiYxxxxxxxxxxxxxxMqtwXnc").getRange(dataRangeNotation).getValues();
    
     requests = [{
     replaceAllText: {
     containsText: {
     text: '{fixtureOneMatch}',
     matchCase: false
     },
    replaceText: value
    }
    }];
    
    To:
    var s = SlidesApp.getActivePresentation();
    s.insertSlide(copydstSlideIndex, src);
    s.getSlides()[0].remove();
    s.saveAndClose();
    var dataRangeNotation = 'slideInfo!B2:B2';
    var value = SpreadsheetApp.openById("###").getRange(dataRangeNotation).getValue();
    var requests = [{
      replaceAllText: {
        containsText: {
          text: '{fixtureOneMatch}',
        },
        replaceText: value,
      }
    }];
    Slides.Presentations.batchUpdate({requests: requests}, s.getId());
    

    References: