LoginSignup
0
0

More than 3 years have passed since last update.

GASでDocumentにランダムな画像を挿入する

Posted at

GASの練習

  1. MyFunction() ... OCRでテキストを取り込んだとき、どうしても色が変わった形になっているので、書式をリセットするもの。
  2. 画像を取り込んだり、消去したりするもの。
function insertRandomPicture() {
  const doc = DocumentApp.getActiveDocument();
  let body = doc.getBody();
  let image = UrlFetchApp.fetch("https://picsum.photos/536/354");

  let bodyImage = body.getImages();

  if (bodyImage.length !== 0) {
    bodyImage[0].removeFromParent();
  }
  body.insertImage(0, image);
}

このコードのダメなところ

思いつくままに。

  • えっと、まずは…なんだろう、ダメなところ。というより改善できるところなんだけど
  • まず、insert...のURLかな
  • まあ、あとはあれだ
  • if の中
  • bodyImage.lenght !== 0としているけど
  • ほんとうはもっとなんかあるはず。if のコードの中が長くてはいけない。
  • これはruby脳なのかな。
  • if ( bodyImages.empty? )を書きたい。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0