0

I 'm very new to both Apps Script and coding in general.

I'm trying to take a google slides presentation, and merge with a csv to create multiple images.

I cannot get the merge function to work. I'm pretty sure I am not loading the csv data correctly - I can get it to show the data range in the execution log but it does not merge into the new presentation. I know I have my tags correct, because I've used them in a 3rd party extension and it works.

I know I'm probably just missing something stupid, but I cannot figure it out. Any help would be appreciated!

Below is the code I currently have:

Update 1

I was able to figure out how to call the data correctly, and am now able to replace the text correctly. However, when I use replaceAllShapesWithImage I get the following error: GoogleJsonResponseException: API call to slides.presentations.batchUpdate failed with error: Invalid requests[2].replaceAllShapesWithImage: There was a problem retrieving the image. The provided image should be publicly accessible, within size limit, and in supported formats.

The images are being called from google drive, and are accessible by anyone with the link. Any ideas? Updated code below:

const spreadsheetId = '1JSXC0XrfUAtcRLXCgVnB-SAQjwk_-YG7W_kGefowONE';
const thetemplateId = '1Pug2cPiGsPL9iKPEnBAhvBVqfSTMyJERCRaSGkyFOr0';
const dataRange = 'Monthly Top Producers!A2:F';

function generateTopPro(){
  var Presentation=SlidesApp.openById(thetemplateId);
  let values = SpreadsheetApp.openById(spreadsheetId).getRange(dataRange).getValues();

for (let i = 0; i < values.length; ++i) {
      const row = values[i];
      const agent_name = row[0]; // name in column 1
      const agent_phone = row[3]; // phone in column 4
      const agent_photo = row[4]; // agent photo url column 5
      const logo_state = row[5]; // state logo url column 6

      // Duplicate the template presentation using the Drive API.
      const copyTitle = agent_name + ' September';
      let copyFile = {
        title: copyTitle,
        parents: [{id: 'root'}]
      };
      copyFile = Drive.Files.copy(copyFile, templateId);
      const presentationCopyId = copyFile.id;

      // Create the text merge (replaceAllText) requests for this presentation.
      const requests = [{
        replaceAllText: {
          containsText: {
            text: '{{agent_name}}',
            matchCase: true
          },
          replaceText: agent_name
        }
      }, {
        replaceAllText: {
          containsText: {
            text: '{{agent_phone}}',
            matchCase: true
          },
          replaceText: agent_phone
        }
      }, {
        replaceAllShapesWithImage: {
        imageUrl: agent_photo,
        imageReplaceMethod: 'CENTER_INSIDE',
        containsText: {
          text: '{{agent_photo}}',
          matchCase: true
        }
      }
    }, {
      replaceAllShapesWithImage: {
        imageUrl: logo_state,
        imageReplaceMethod: 'CENTER_INSIDE',
        containsText: {
          text: '{{logo_state}}',
          matchCase: true
        }
      }
      }];

      // Execute the requests for this presentation.
      const result = Slides.Presentations.batchUpdate({
        requests: requests
      }, presentationCopyId);
      // Count the total number of replacements made.
      let numReplacements = 0;
      result.replies.forEach(function(reply) {
        numReplacements += reply.replaceAllText.occurrencesChanged;
      });
      console.log('Created presentation for %s with ID: %s', agent_name, presentationCopyId);
      console.log('Replaced %s text instances', numReplacements);
    }


}
  • I have to apologize for my poor English skill. Unfortunately, I cannot understand the relationship between `I'm pretty sure I am not loading the csv data correctly` and your showing script and your goal. Can I ask you about the detail of it? – Tanaike Oct 09 '22 at 00:16
  • I was missing the `let values` function. But now I'm getting an api call error for my google drive images. – Chelsea Ziss Oct 09 '22 at 14:52
  • Thank you for replying. I would like to support you. But, I have to apologize for my poor English skill, again. Unfortunately, I cannot still understand your question. But I would like to try to understand it. When I could correctly understand it, I would like to think of a solution. I would be grateful if you can forgive my poor English skill. – Tanaike Oct 09 '22 at 23:49

1 Answers1

0

There was a problem retrieving the image. The provided image should be publicly accessible, within size limit, and in supported formats is a known issue when trying to use an image from Google Drive

If no workaround work for you, the only two things you can do in the current state are:

  • "Star" the feature request to increase visibility which will hopefully accelerate implementation
  • Use the thumbnailLink that oyu can retrieve with Files: get as imageUrl - this gives you an image in low quality, but better than nothing
ziganotschka
  • 25,866
  • 2
  • 16
  • 33