Created
January 27, 2024 16:08
-
-
Save swfz/ec323055b7b3dff606b516ea0ca7aecc to your computer and use it in GitHub Desktop.
GoogleSlideでスライド内リンクの末尾にページ数を付与する(更新の場合は最新情報にする)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function onInstall(e) { | |
onOpen(); | |
} | |
function onOpen(e) { | |
SlidesApp.getUi().createAddonMenu() | |
.addItem('Update TOC Link Page Number', 'updateTocLinkPageNumber') | |
.addToUi(); | |
} | |
function getTargetPageNumber(title) { | |
const ui = SlidesApp.getUi() | |
const res = ui.prompt(title,"Please Input Target Page Number?", ui.ButtonSet.OK_CANCEL) | |
if (res.getSelectedButton() === ui.Button.OK){ | |
return parseInt(res.getResponseText()) | |
} | |
else { | |
Logger.log('Failed.') | |
} | |
} | |
function updateTocLinkPageNumber() { | |
const presentation = SlidesApp.getActivePresentation() | |
const slides = presentation.getSlides() | |
const tocSlide = slides[getTargetPageNumber('Update TOC Link Page Number') - 1] | |
const shapes = tocSlide.getShapes().filter(s => s.getShapeType() === SlidesApp.ShapeType.TEXT_BOX) | |
shapes.forEach(shape => { | |
const textRange = shape.getText() | |
const paragraphs = textRange.getParagraphs().filter(p => { | |
const links = p.getRange().getLinks() | |
if (links.length === 0) { | |
return false | |
} | |
return links[0].getTextStyle().getLink().getLinkType() === SlidesApp.LinkType.SLIDE_ID | |
}) | |
paragraphs.forEach(paragraph => { | |
const textRange = paragraph.getRange() | |
const link = textRange.getLinks()[0].getTextStyle().getLink() | |
const slideId = link.getSlideId() | |
const pageIndex = slides.findIndex(s => s.getObjectId() === slideId) + 1 | |
const slide = slides.find(s => s.getObjectId() === slideId) | |
const text = textRange.asString() | |
const pageNumPattern = / \(p\d+\)/ | |
const match = text.match(pageNumPattern) | |
if (match){ | |
textRange.replaceAllText(match[0], '') | |
} | |
const pageNumText = ` (p${pageIndex})` | |
const textLength = paragraph.getRange().asString().length | |
// -1しないと改行文字列が含まれてしまうため改行後に注釈が含まれてしまう | |
textRange.insertText(textLength - 1, pageNumText) | |
textRange.getTextStyle().setLinkSlide(slide) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment