Google Slides: Page; Generate PNG Image

Google Slides: Page; Generate PNG Image

Generates an image of the specified page in the presentation with generating a URL to the image which has a lifetime of 30 minutes. Note that if the presentation sharing settings are changed, access to the images may be lost.

2019-09-02 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/google-slides-page-generate-png-image/

Configs
  • A: Select OAuth2 Config Name (at [OAuth 2.0 Setting]) *
  • B: Set PRESENTATION-ID to Retrieve (44 chars in File URI) * #{EL}
  • C: Set OBJECT-ID of SLIDE (Chars following “id.”) * #{EL}
  • D: Select FILES DATA for Slide Image (update) *
  • E: Set FILE NAME if you want to save as a different name #{EL}
  • F: Select STRING DATA for URL to download (update)
Script
// (c) 2019, Questetra, Inc. (the MIT License)

//// == OAuth2 Setting example ==
// Authorization Endpoint URL:
//  "https://accounts.google.com/o/oauth2/auth?access_type=offline&approval_prompt=force"
// Token Endpoint URL:
//  "https://accounts.google.com/o/oauth2/token"
// Scope:
//  "https://www.googleapis.com/auth/presentations https://www.googleapis.com/auth/spreadsheets.readonly"
// Client ID:
//  ( from https://console.developers.google.com/ )
// Consumer Secret:
//  ( from https://console.developers.google.com/ )
//  *Redirect URLs: "https://s.questetra.net/oauth2callback"


//////// START "main()" ////////
main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const oauth2   = configs.get( "conf_OAuth2"  ) + "";
const presentationId = configs.get( "conf_PresentationId") + "";
const objectId = configs.get( "conf_ObjectId") + "";
const dataIdD = configs.get( "conf_DataIdD" ) + "";
let   saveAs  = configs.get( "conf_SaveAs") + "";
const dataIdF = configs.get( "conf_DataIdF" ) + "";
// 'java.lang.String' (String Obj) to javascript primitive 'string'
if( saveAs === "" ){
  saveAs = processInstance.getProcessInstanceId() + ".png";
}
engine.log( " AutomatedTask Config: Presentation ID: " + presentationId );
engine.log( " AutomatedTask Config: Object ID: " + objectId );


//// == Data Retrieving / ワークフローデータの参照 ==
var tmpFiles = engine.findDataByNumber( dataIdD ); // java.util.ArrayList
if( tmpFiles === null ) {
  engine.log( " AutomatedTask FilesArray {D}: empty" );
  tmpFiles = new java.util.ArrayList();
}else{
  engine.log( " AutomatedTask FilesArray {D}: " + 
              tmpFiles.size() + " files" );
}


//// == Calculating / 演算 ==
/// obtain OAuth2 Access Token
const token   = httpClient.getOAuth2Token( oauth2 );

/// get Page Thumbnail
let apiRequest = httpClient.begin(); // HttpRequestWrapper
apiRequest = apiRequest.bearer( token );
const apiUri = "https://slides.googleapis.com/v1/presentations/" +
                presentationId + "/pages/" +
                objectId + "/thumbnail";
engine.log( " AutomatedTask Trying: GET " + apiUri );
const response = apiRequest.get( apiUri );
const responseCode = response.getStatusCode() + "";
engine.log( " AutomatedTask ApiResponse: Status " + responseCode );
if( responseCode !== "200"){
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
         responseCode + "\n" + response.getResponseAsString() + "\n" );
}
const responseStr = response.getResponseAsString() + "";
const responseObj = JSON.parse( responseStr );
engine.log( " AutomatedTask ApiResponse: width " + responseObj.width );
engine.log( " AutomatedTask ApiResponse: height " + responseObj.height );

/// get Page Thumbnail (PNG Image)
let apiRequest2 = httpClient.begin(); // HttpRequestWrapper
const apiUri2 = responseObj.contentUrl;
engine.log( " AutomatedTask Trying: GET " + apiUri2 );
const response2 = apiRequest2.get( apiUri2 );
const responseCode2 = response2.getStatusCode() + "";
engine.log( " AutomatedTask ApiResponse: Status " + responseCode2 );
if( responseCode2 !== "200"){
  throw new Error( "\n AutomatedTask UnexpectedResponseError: " +
         responseCode2 + "\n" + response2.getResponseAsString() + "\n" );
}
const qfile = new com.questetra.bpms.core.event.scripttask.NewQfile(
    saveAs, response2.getContentType(), response2.getResponse()
  );
tmpFiles.add( qfile );


//// == Data Updating / ワークフローデータへの代入 ==
engine.setDataByNumber( dataIdD, tmpFiles );
if( dataIdF !== "" ){
  engine.setDataByNumber( dataIdF, responseObj.contentUrl );
}

} //////// END "main()" ////////

Download

Capture

Notes

  1. Presentation ID: https://docs.google.com/presentation/d/1p33hGJFUNYixBmMeaV81nsOVYGUUrZIFyErinFp3CI8/edit#slide=id.g613777c84a_0_0
  2. Slide Object ID: https://docs.google.com/presentation/d/1p33hGJFUNYixBmMeaV81nsOVYGUUrZIFyErinFp3CI8/edit#slide=id.g613777c84a_0_0
  3. Default File Name: #{processInstanceId}.png
  4. The image size is determined automatically regardless of the Page Setup. (As of September 2019)
  5. If size specification is required, please rewrite “=s1600” URL to “=s1200”.

See also

1 thought on “Google Slides: Page; Generate PNG Image”

  1. Pingback: Google Slides: Page; Duplicate – Questetra Support

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: