使用Google Apps Script在Google幻灯片中创建矩形网格

huangapple go评论62阅读模式
英文:

Rectangular Grid Using Google Apps Script in Google Slides

问题

以下是已翻译的部分:

所以,我请ChatGPT使用Google Apps Script在Google幻灯片中创建了一个矩形网格。以下是我给它的参数:该网格有5列和4行,每个矩形的宽度不超过网格宽度的20%,网格宽度为1880像素。矩形之间以20像素的边距分隔,幻灯片本身在外部有20像素的边距。它在某种程度上起作用了,但我不得不在脚本中调整宽度和高度,因为矩形超出了1920 x 1080的幻灯片的宽度和高度。此外,当我调整边距和列数时,它并不完全符合我的要求。以下是它给我的代码...

这是它给我的网格的截图。我不得不调整SlideWidth和SlideHeight才能使其工作。

[![使用Google Apps Script在Google幻灯片中创建的矩形网格][1]][1]

所以,我知道脚本的第7-9行中使用的公式是错误的,但我不够聪明来更改它,以便无论我放入多大的边距,或者多少行或列,都可以适应在具有20像素边距的1920 x 1080幻灯片内。

**你们中是否有人知道如何编辑此脚本,以便无论我放入多大的边距,或者多少行或列,矩形网格都适应在具有20像素边距的1920 x 1080幻灯片内?**

[这是一个链接,您可以制作自己的副本以查看脚本及其结果。][2]

  [1]: https://i.stack.imgur.com/CkSsa.png
  [2]: https://docs.google.com/presentation/d/1d2P-hyex7ayECdY2RGoLyd8Nhez7NWaBNokMT1fN_nI/copy
英文:

So, I asked ChatGPT to create a rectangular grid using Google Apps Script within Google Slides. Here are the parameters I gave it: The grid has 5 columns and 4 rows, with each rectangle being no more than 20% of the width of the grid, which is 1880px wide. The rectangles are separated by 20px margins and the slide itself has 20px margins on the outside. It sort of worked, but I had to adjust the width and height in the script because the rectangles went beyond the width and height of the 1920 x 1080 slide. Also, when I adjust the margin and number of columns, it doesn't fit exactly like I want it to. Here's the code below that it gave me...

function createGrid() {
  var slideWidth = 1440;
  var slideHeight = 810;
  var margin = 20;
  var numRows = 4;
  var numColumns = 6;
  var maxRectWidth = slideWidth * 0.2; // Each rectangle is no more than 20% of the width of the grid
  var rectWidth = Math.min((slideWidth - margin * 2 - margin * (numColumns - 1)) / numColumns, maxRectWidth);
  var rectHeight = (slideHeight - margin * 2 - margin * (numRows - 1)) / numRows;
  
  // Create new slide and set dimensions
  var presentation = SlidesApp.getActivePresentation();
  var slide = presentation.appendSlide();
  slide.selectAsCurrentPage();

  // Create grid of rectangles
  for (var row = 0; row < numRows; row++) {
    for (var column = 0; column < numColumns; column++) {
      var x = margin + column * (rectWidth + margin);
      var y = margin + row * (rectHeight + margin);
      var shape = slide.insertShape(SlidesApp.ShapeType.RECTANGLE, x, y, rectWidth, rectHeight);
      shape.getBorder().setTransparent();
      shape.getFill().setSolidFill('#777777');
    }
  }
}

Here's a screenshot of the grid it gave me. I had to adjust the SlideWidth and SlideHeight to get it to work.

使用Google Apps Script在Google幻灯片中创建矩形网格

So, I know that the math is wrong with the formula it's using in lines 7-9 of the script, but I'm not smart enough to change it so that no matter what margins I put in there, or number of columns or rows, it would fit within the 1920 x 1080 slide with 20px margins on the outside of the slide.

Would any of you know how to edit this script so that no matter what margin, or number of rows or columns I put in there, the rectangular grid would fit within a 1920 x 1080 slide with 20px margins on the outside?

Here's a link to make your own copy to view the script and its result.

答案1

得分: 2

你确定幻灯片的大小吗?如果不同的浏览器生成了一个更小的幻灯片怎么办?为什么不试试这个。

let presentation = SlidesApp.getActivePresentation();
let slides = presentation.getSlides();
let slide = slides[0];

let slideWidth = presentation.getPageWidth();
let slideHeight = presentation.getPageHeight();
let margin = 20;
let numRows = 5;
let numColumns = 4;
let rectWidth = (slideWidth - margin * 2 - margin * (numColumns - 1)) / numColumns;
let rectHeight = (slideHeight - margin * 2 - margin * (numRows - 1)) / numRows;

// 创建矩形网格
for (let row = 0; row < numRows; row++) {
  for (let column = 0; column < numColumns; column++) {
    let x = margin + column * (rectWidth + margin);
    let y = margin + row * (rectHeight + margin);
    let shape = slide.insertShape(SlidesApp.ShapeType.RECTANGLE, x, y, rectWidth, rectHeight);
    shape.getBorder().setTransparent();
    shape.getFill().setSolidFill('#777777');
  }
}
英文:

Are you sure about the size of the slide? What if a different browser produces a smaller slide. Why not try this.

let presentation = SlidesApp.getActivePresentation();
let slides = presentation.getSlides();
let slide = slides[0];

let slideWidth = presentation.getPageWidth();
let slideHeight = presentation.getPageHeight();
let margin = 20;
let numRows = 5;
let numColumns = 4;
let rectWidth = (slideWidth - margin * 2 - margin * (numColumns - 1)) / numColumns;
let rectHeight = (slideHeight - margin * 2 - margin * (numRows - 1)) / numRows;

// Create grid of rectangles
for (let row = 0; row < numRows; row++) {
  for (let column = 0; column < numColumns; column++) {
    let x = margin + column * (rectWidth + margin);
    let y = margin + row * (rectHeight + margin);
    let shape = slide.insertShape(SlidesApp.ShapeType.RECTANGLE, x, y, rectWidth, rectHeight);
    shape.getBorder().setTransparent();
    shape.getFill().setSolidFill('#777777');
  }
}

huangapple
  • 本文由 发表于 2023年2月18日 23:42:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75494448.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定