Googleドライブにある画像ファイルをドキュメントに配置したくて書いたコードです。
画像の大きさがドキュメントのページ範囲に収まるように、getPageWidth, getPageHeightを使いました。
画像の大きさがドキュメントのページ範囲に収まるように、getPageWidth, getPageHeightを使いました。
コード.gs
const docUrl = "https://docs.google.com/document/d/ID/edit"; const imageUrl = "https://drive.google.com/file/d/ID/view?usp=sharing"; const imageId = imageUrl.split("/d/")[1].split("/view?")[0] const image = DriveApp.getFileById(imageId); function myFunction() { const doc = DocumentApp.openByUrl(docUrl); const docBody = doc.getBody(); const docWidth = docBody.getPageWidth(); const docHeight = docBody.getPageHeight(); docBody.getParagraphs()[0].insertInlineImage(0, image).setWidth(docWidth).setHeight(docHeight); } |
参考
insertInlineImage(childIndex, image)
Class InlineImage
Class Body