在Google表格中,我该如何编写脚本以将每个图形移动到表格上的特定位置? [英] In Google Sheets how can I write a script to move each drawing to a specific location on the sheet?

查看:76
本文介绍了在Google表格中,我该如何编写脚本以将每个图形移动到表格上的特定位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Google表格,其中包含四个图形(即形状).什么脚本可以将每个形状移动到图纸上的指定位置?

这里是一个示例的链接,在该示例中,我想单击蓝色矩形,并让脚本将绿色矩形移至单元格A1,将紫色三角形移至单元格D7,将橙色圆移至单元格I3./p>

  • 通过单击蓝色矩形"运行以下示例脚本.

  • 2.示例脚本.

      function myFunction(){//1.准备用于搜索工程图的对象.const obj = {"10":{名称:绿色矩形",moveTo:[1,1]},//< --- A1"13":{name:紫色三角形",moveTo:[7,4]},//< --- D7"18":{名称:橙色圆圈",moveTo:[3,9]},//< --- I3};//2.检索工作表.const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");//3.移动工程图.sheet.getDrawings().forEach(d => {const arow = d.getContainerInfo().getAnchorRow();如果(在obj中长大){d.setPosition(... obj [arow] .moveTo,0,0);}})} 

    • 在这种情况下,蓝色矩形的功能名称为 myFunction .然后使用锚点单元作为标识符来检索每个图形.
    • 要运行此脚本,请单击设置 myFunction 的蓝色矩形.这样,每个图形都将移动.

    结果:

    为上面的输入图像运行脚本时,可以获得以下输出.

    注意:

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

    参考:

    I have a Google sheet with four drawings i.e. shapes. What script would move each shape to a designated location on the sheet?

    Here is a link to an example where I'd like to click on the blue rectangle and have the script move the green rectangle to cover cell A1, the purple triangle to cell D7, and the orange circle to cell I3.

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

    I can't find a way to select specific drawings.

    解决方案

    I believe your goal as follows.

    • You want to move the drawing as follows, when the blue rectangle is clicked.
      • Move the green rectangle to the cell "A1".
      • Move the purple triangle to the cell "D7".
      • Move the orange circle to the cell "I3".

    For this, how about this answer?

    Issue and workaround:

    Unfortunately, in the current stage, each drawing cannot be distinguished by the object color and the identifier. So in order to achieve your goal, as a workaround, I would like to propose to put each drawing to the predetermined cells and use each drawing the anchor cell as the identifier.

    Usage:

    1. Set function name.

    1. Copy and paste the following sample script.
    2. At first, please set the function name of myFunction to "the blue rectangle".
    3. As the sample predetermined cells for each drawings, please put the upper left of "the green rectangle", "the purple triangle" and "the orange circle" in the cells "A10", "A13" and "A18", respectively. You can see it at the following image.

    4. Run the following sample script by clicking "the blue rectangle".

    2. Sample script.

    function myFunction() {
      // 1. Prepare an object for searching Drawings.
      const obj = {
        "10": {name: "green rectangle", moveTo: [1, 1]}, // <--- A1
        "13": {name: "purple triangle", moveTo: [7, 4]}, // <---  D7
        "18": {name: "orange circle", moveTo: [3, 9]}, // <---  I3
      };
    
      // 2. Retrieve sheet.
      const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
    
      // 3. Move drawings.
      sheet.getDrawings().forEach(d => {
        const arow = d.getContainerInfo().getAnchorRow();
        if (arow in obj) {
          d.setPosition(...obj[arow].moveTo, 0, 0);
        }
      })
    }
    

    • In this case, the blue rectangle has the function name of myFunction. And each drawings are retrieved using the anchor cells as the identifier.
    • In order to run this script, please click the blue rectangle that myFunction was set. By this, each drawing is moved.

    Result:

    When the script is run for above input image, the following output can be obtained.

    Note:

    • This is a sample script. So please modify this for your actual situation.

    Reference:

    这篇关于在Google表格中,我该如何编写脚本以将每个图形移动到表格上的特定位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆