LANG SELRCT

Apps Script Reference  (Create: Create new Spreadsheet | Create new Apps Script

Saturday, May 7, 2022

Google Apps Scriptでドキュメントに画像を配置したい


Googleドライブにある画像ファイルをドキュメントに配置したくて書いたコードです。

画像の大きさがドキュメントのページ範囲に収まるように、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

Latest post

スプレッドシートA列にある複数のテキストをスライドに追加したい(Google Apps Script)

今回Google Apps Scriptでやりたいこと GoogleスプレッドシートA列にある複数の値を取得して Googleスライドに渡して 図形オブジェクトのテキストとして追加したい ① スプレッドシートのA列に値を入れておく ② Code.gsのinsertNewShape...