如何在Google表格中编写脚本以将图形移动到表格上的特定位置?

时间:2020-05-01 17:53:49

标签: google-apps-script google-sheets google-sheets-macros

我有一个带有图纸(即形状)的Google表格。什么脚本可以将形状移动到图纸上的指定位置?

这里是一个示例的链接,在该示例中,我想单击蓝色矩形,并使脚本将绿色矩形移动到覆盖单元格A1。

https://drive.google.com/open?id=1eFCwDiY90ZM5SIMa6C0rSdhG_oC-SWgsk8vAvjIR34s

这是我在Google中的第一个脚本,我找不到选择图形的方法。

1 个答案:

答案 0 :(得分:1)

我相信您的目标如下。

  • 您想在单击蓝色矩形时将图形(即绿色矩形)移动到单元格“ A1”。

为此,这个答案如何?

用法:

1。设置函数名称。

首先,请将myFunction的函数名称设置为“蓝色矩形”。这样,当单击蓝色矩形时,将运行myFunction

2。示例脚本。

function myFunction() {
  // 1. Retrieve sheet.
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");

  // 2. Retrieve "the green rectangle".
  const drawing = sheet.getDrawings().filter(e => e.getOnAction() != "myFunction")[0];

  // 3. Move "the green rectangle".
  drawing.setPosition(1, 1, 0, 0);
}
  • 在这种情况下,蓝色矩形的功能名称为myFunction。因此,使用它可以检索绿色矩形。
  • 要运行此脚本,请单击设置了myFunction的蓝色矩形。这样,绿色矩形将移动到单元格“ A1”。

注意:

  • 这是一个示例脚本。因此,请根据您的实际情况对此进行修改。

参考:

  • enter image description here