InsideDarkWeb.com

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

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.

Stack Overflow Asked by Botanist20 on November 22, 2021

1 Answers

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!

Related Questions

Delphi Parse JSON

2  Asked on February 17, 2021 by stanley-mladenek

   

How to create a filter using bootstrap and jQuery

2  Asked on February 17, 2021 by jefin-winston

     

Change Canvas sizes with zoom

0  Asked on February 17, 2021 by reit-abdullah-yavuzkol

 

Is there a way to make the Loop Code make it faster?

4  Asked on February 17, 2021 by levesque-xylia

 

Using libcurl in g++

0  Asked on February 16, 2021 by marcel-kopera

   

how to count number of file with a matching pattern using python

2  Asked on February 16, 2021 by suryansh-mathur

 

R First Row By Group When Condition Is Met

4  Asked on February 16, 2021 by bvowe

   

C++ program won’t print anything with vectors?

2  Asked on February 16, 2021 by luckylone-official

 

What is damping for?

3  Asked on February 16, 2021 by lix

       

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.