简体   繁体   中英

Image and Data Merge Sheets to Slides Apps Script - Update

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);
    }


}

There was a problem retrieving the image. The provided image should be publicly accessible, within size limit, and in supported formats 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM