Search code examples
google-apps-scriptgoogle-slides-apigoogle-slides

Google Slides Tables: setColumnWidth


in Google Slides you can add a Table and each Column in said table can be configured to a certain length in my case i want each column to be column1: 50pts column2: 200pts column3: 30pts column4: 200pts column5: 200pts

i cant seem to find a way to customize the width of a column in the docs can someone help? i have tried with

var cellPadding = cell.getCellProperties().getPadding();
cellPadding.setLeft(targetColumnWidth / 2);
cellPadding.setRight(targetColumnWidth / 2);
cell.getCellProperties().setPadding(cellPadding);

and

column.setWidth(targetColumnWidth);

neither of them works since column.setWidth() nor cell.getCellProperties() exist Please help


Solution

  • Issue and workaround:

    In the current stage, it seems that the width of the individual table row and column cannot be managed by the Google Slides service (SlidesApp). But, fortunately, when Slides API is used, it seems that your goal can be achieved. In this answer, I would like to propose a sample script using Slides API.

    Sample script:

    Please copy and paste the following script to the script editor of Google Slides, and save the script. Before you use this script, please enable Google Slides API at Advanced Google services.

    function myFunction() {
      const widths = [50, 200, 32, 200, 200]; // Widths for columns 1 to 5. These values are from your question.
    
      const s = SlidesApp.getActivePresentation();
      const slide = s.getSlides()[0]; // 1st page is used as a sample.
      const table = slide.getTables()[0]; // 1st table is used.
    
      const objectId = table.getObjectId();
      const requests = widths.map((e, i) => ({
        updateTableColumnProperties: {
          objectId, columnIndices: [i],
          tableColumnProperties: { columnWidth: { magnitude: e, unit: "PT" } },
          fields: "columnWidth"
        }
      }));
      Slides.Presentations.batchUpdate({ requests }, s.getId());
    }
    

    Testing:

    When this script is used, the following result is obtained.

    enter image description here

    Note:

    • In the current stage, it seems that the minimum width of a column using Slides API is 32 points. When 30 points are used, an error like The table column width must be at least 406400 EMU (32 points). occurs. So, I used 32 instead of 30 in your question. Please be careful about this.

    References: