TransWikia.com

TypeError: Cannot read property 'getSelectionType'; Automatically resize an inserted chart in google slides

Stack Overflow Asked by Botanist20 on November 22, 2021

I am very new to the community and this is my first question so pleace be patient!

I am trying to run a script, which can resize the inserted charts to s specific width/height.
My ultimate goal would be to resize a prior inserted chart upon a keypress but i can’t get there so this is my workaround.

  function resizeCharts() {
  var selectedObjects = SlidesApp.getActivePresentation().getSelection();
  if (selectedObjects.getSelectionType.CURRENT_PAGE() == SlidesApp.SelectionType.PAGE_ELEMENT) {
    var pageElements = selectedObjects.getPageElementRange().getPageElements();
    for (var i = 0; i < pageElements.length; i++) {
      if (pageElements[i].getPageElementType() == SlidesApp.PageElementType.sheetsChart) {
        pageElements[i].select(true);
        var chart = pageElements[i];
        var newWidth = 4.48; 
        var newHeight = 3.69;
        chart.setWidth(newWidth).setHeight(newHeight);
        var ui = SlidesApp.getUi();
        pageElements[i].select(false);
      }
    }
  }
}

But i cannot solve the following error message:

TypeError: Cannot read property ‘getSelectionType’ of null
at rearrangeCharts(Code:3:23)

I am very new to google app scripting and java script!
Any help is truly appreciated. Thank you.

PS: What would be the most efficient way to resize a chart automatically? Is there a way to trigger the function by keypress? Currently, i am triggering the function by time.

One Answer

First of all, you need to make the following changes to your resizeCharts() function:

function resizeCharts() {
  var selectedObjects = SlidesApp.getActivePresentation().getSelection();

  if (selectedObjects.getCurrentPage() != null) {
    var pageElements = selectedObjects.getPageElementRange().getPageElements();
    for (var i = 0; i < pageElements.length; i++) {
      if (pageElements[i].getPageElementType().toString( ) == "SHEETS_CHART") {
        pageElements[i].select(true);
        var chart = pageElements[i];
        var newWidth = 250.48; 
        var newHeight = 250.48;
        chart.setWidth(newWidth).setHeight(newHeight);
        var ui = SlidesApp.getUi();
        pageElements[i].select(false);
      }
    }
  }
}

then you can add the following code snippet to the existing script as a separate function to create a macro menu button that allows you to execute the script from your presentation file:

function onOpen(e) {
  SlidesApp.getUi() 
      .createMenu('Resize Chart')
      .addItem('Resize Selected Chart', 'resizeCharts')
      .addToUi();
} 

Please be aware that if you want the script to be executed successfuly, you need first to click on the chart you want to resize.

I chose different dimensions so the chart can be visible.

Answered by soMario on November 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP