node.jsgoogle-slides-api

Reordering all slides


I have a NodeJS app I am writing that will manipulate slides in my Google Slides Presentation. I am trying to reorder all the slides in my Google Slides presentation at one time. I keep getting this error: reason: GaxiosError: Invalid requests[0].updateSlidesPosition: The slides should be in presentation order, with no duplicates. I don't even know what "in presentation order" means.

I have 11 slides in my presentation with ID's looking like "Sermon-Slide-9","Sermon-Slide-3","Sermon-Slide-0", "Sermon-Slide-1", etc. And I wanted them placed in numerical order so Sermon-Slide-0, Sermon-Slide-1, Sermon-Slide-2, etc.

Here is the function that is doing the reordering:

const reorderSlidesNumerically = async (slideCount, slidesApi, fileID) => {
  let slideObjectIds = [];
  for (let i = 0; i < slideCount; i++) {
    slideObjectIds.push('Sermon-Slide-' + i);
  }

  console.log({ slideObjectIds });
  const requests = [{
    "updateSlidesPosition": {
      "slideObjectIds": [...slideObjectIds],
      "insertionIndex": 0
    }
  }];
  console.log({requests});
  const batchUpdateRequest = { requests };
  await slidesApi.presentations.batchUpdate({
    presentationId: fileID,
    requestBody: batchUpdateRequest,
  });
};

And here is part of the response I am receiving back:

 body: '{"requests":[{"updateSlidesPosition":{"slideObjectIds":["Sermon-Slide-0","Sermon-Slide-1","Sermon-Slide-2","Sermon-Slide-3","Sermon-Slide-4","Sermon-Slide-5","Sermon-Slide-6","Sermon-Slide-7","Sermon-Slide-8","Sermon-Slide-9","Sermon-Slide-10"],"insertionIndex":0}}]}',
    responseType: 'json',
    retryConfig: {
      currentRetryAttempt: 0,
      retry: 3,
      httpMethodsToRetry: [Array],
      noResponseRetries: 2,
      statusCodesToRetry: [Array]
    }
  },
  code: 400,
  errors: [
    {
      message: 'Invalid requests[0].updateSlidesPosition: The slides should be in presentation order, with no duplicates.',
      domain: 'global',
      reason: 'badRequest'
    }
  ]
}

Solution

  • Modification points:

    In order to avoid this, how about the following modification?

    From:

    const requests = [{
      "updateSlidesPosition": {
        "slideObjectIds": [...slideObjectIds],
        "insertionIndex": 0
      }
    }];
    

    To:

    const requests = slideObjectIds.reverse().map((e) => ({
      updateSlidesPosition: { slideObjectIds: [e], insertionIndex: 0 },
    }));
    

    Reference: