问题
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.
回答1:
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.
- Copy and paste the following sample script.
- At first, please set the function name of
myFunction
to "the blue rectangle". 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.
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:
- Class Drawing
来源:https://stackoverflow.com/questions/61553339/in-google-sheets-how-can-i-write-a-script-to-move-each-drawing-to-a-specific-loc