Skip to content

Instantly share code, notes, and snippets.

@swfz
Last active April 6, 2024 19:31
Show Gist options
  • Save swfz/545b1c2b6234ed8886477bfbfd167f42 to your computer and use it in GitHub Desktop.
Save swfz/545b1c2b6234ed8886477bfbfd167f42 to your computer and use it in GitHub Desktop.
GoogleSlideで画像などに強調したい部分以外に半透明の図形を被せるアドオン
function onInstall(e) {
onOpen();
}
function onOpen(e) {
SlidesApp.getUi().createAddonMenu()
.addItem('Hightlight(Black)', 'highlightBlack')
.addItem('Hightlight(White)', 'highlightWhite')
.addToUi();
}
function highlightBlack() {
highlight('#000000')
}
function highlightWhite() {
highlight('#FFFFFF')
}
// ImageやShape上にあるtargetというテキストのShapeの範囲を強調する
function highlight(color) {
const presentation = SlidesApp.getActivePresentation();
const pageIndex = getTargetPageIndex(`Highlight ${color}`)
const slides = presentation.getSlides();
const slide = slides[pageIndex];
const objects = [...(slide.getShapes().filter(s => s.getShapeType() !== SlidesApp.ShapeType.TEXT_BOX)), ...(slide.getImages()), ...(slide.getSheetsCharts())]
const targetShapes = slide.getShapes().filter(s => s.getText().asString() === 'target\n')
const cordinate = (s) => {
return {
top: s.getTop(),
bottom: s.getTop() + s.getHeight(),
left: s.getLeft(),
right: s.getLeft() + s.getWidth()
}
}
const isBehind = (shape, object) => {
const front = cordinate(shape)
const back = cordinate(object)
// frontがbackに内包されている状態
if (front.top > back.top && front.bottom < back.bottom && front.left > back.left && front.right < back.right) {
return true
}
return false
}
targetShapes.forEach(s => {
const behindObject = objects.find(o => isBehind(s, o))
const lefts = [
behindObject.getLeft(),
s.getLeft(),
s.getLeft() + s.getWidth()
]
const tops = [
behindObject.getTop(),
s.getTop(),
s.getTop() + s.getHeight()
]
const widths = [
s.getLeft() - behindObject.getLeft(),
s.getWidth(),
(behindObject.getLeft() + behindObject.getWidth()) - (s.getLeft() + s.getWidth())
]
const heights = [
s.getTop() - behindObject.getTop(),
s.getHeight(),
(behindObject.getTop() + behindObject.getHeight()) - (s.getTop() + s.getHeight())
]
const hatchShapes = [[true, true, true], [true, false, true], [true, true, true]].map((line, i) => {
return line.map((item, j) => {
if (!item) {
return false
}
const hatchShape = slide.insertShape(
SlidesApp.ShapeType.RECTANGLE,
lefts[i],
tops[j],
widths[i],
heights[j]
)
const fill = hatchShape.getFill()
fill.setSolidFill(color, 0.4)
const border = hatchShape.getBorder()
border.setTransparent()
return hatchShape
}).filter(v => v)
}).flat()
slide.group(hatchShapes)
s.remove()
})
}
@swfz
Copy link
Author

swfz commented Mar 10, 2024

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment