0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【GAS】スプレッドシートのデータをGoogleドキュメントに自動反映

Last updated at Posted at 2025-09-28

〜日報・議事録・アンケート集計・請求書に応用する方法〜

はじめに

Googleスプレッドシートで管理しているデータをGoogleドキュメントに貼り付けて整形する作業、日々の業務でよくありますよね。

手作業だと時間がかかるうえに整形も面倒…。
そこで Google Apps Script (GAS) を使い、自動で「スプシ→ドキュメント」に転送する仕組みを作ります。

さらに今回は 応用シーン(報告書・議事録・アンケート集計・請求書) にも展開できるよう、ミニサンプルコード付きで解説します。

📌 基本コード:スプシをドキュメントに転送

function transferData() {
  try {
    // スプレッドシートを取得
    const sourceId = '【スプレッドシートのID】';
    const ss = SpreadsheetApp.openById(sourceId);
    const sheet = ss.getSheets()[0];

    // データを二次元配列で取得
    const values = sheet.getDataRange().getValues();

    // ドキュメントに書き込み
    const doc = DocumentApp.getActiveDocument();
    const body = doc.getBody();
    const table = body.appendTable(values);

    // ヘッダー行を装飾
    const headerRow = table.getRow(0);
    headerRow.setAttributes({
      [DocumentApp.Attribute.BOLD]: true,
      [DocumentApp.Attribute.BACKGROUND_COLOR]: '#EEEEEE'
    });

  } catch (error) {
    Logger.log("エラーが発生しました:" + error);
  }
}

📌 getValue() vs getValues() の理解

getValue() → 単一セルを返す

getValues() → 範囲を二次元配列で返す

例:

Logger.log(sheet.getRange(1,1).getValue());
// → 100

Logger.log(sheet.getRange(1,1,2,3).getValues());
// → [[100,200,300],[400,500,600]]

👉 スプレッドシートは「行×列」の表形式なので、複数セルを取得すると自動で二次元配列になるわけです。

📌 応用シーンとサンプルコード

  1. 日報・週報の自動生成

社員の日報をスプシに入力 → ドキュメントに整形して提出用に。

function dailyReport() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("日報");
  const values = sheet.getDataRange().getValues();

  const doc = DocumentApp.create("日報レポート");
  const body = doc.getBody();
  body.appendParagraph("📅 日報まとめ");
  body.appendTable(values);
}

2. 議事録の自動化

会議の内容をスプシに記録 → ドキュメントで議事録に整形。

function meetingMinutes() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("議事録");
  const values = sheet.getDataRange().getValues();

  const doc = DocumentApp.create("議事録");
  const body = doc.getBody();
  body.appendParagraph("📝 会議議事録");
  const table = body.appendTable(values);

  // ヘッダーを強調
  table.getRow(0).setAttributes({
    [DocumentApp.Attribute.BOLD]: true,
    [DocumentApp.Attribute.BACKGROUND_COLOR]: '#DDDDDD'
  });
}

3. アンケート結果のまとめ

Googleフォーム→スプシ→ドキュメントへ。共有しやすい形に。

function surveySummary() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("アンケート");
  const values = sheet.getDataRange().getValues();

  const doc = DocumentApp.create("アンケート結果");
  const body = doc.getBody();
  body.appendParagraph("📊 アンケート集計結果");
  body.appendTable(values);
}

4. 見積書・請求書の自動作成

スプシの商品データ→ドキュメントに転送→体裁を整えてPDF化。

function createInvoice() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("請求書データ");
  const values = sheet.getDataRange().getValues();

  const doc = DocumentApp.create("請求書");
  const body = doc.getBody();
  body.appendParagraph("💰 請求書");
  body.appendTable(values);

  // PDF化して保存
  const blob = doc.getAs('application/pdf');
  DriveApp.createFile(blob);
}

✅ まとめ

  • getValue() は単一セル、getValues() は二次元配列(表全体)を返す

  • GASで「スプシ→ドキュメント転送」を自動化すれば、

  • 日報・週報

  • 議事録

  • アンケート結果まとめ

  • 見積書/請求書
    など業務効率化に直結する

👉 学習体験や気づきは note にまとめています。
コード以外の「どう感じたか」も知りたい方はこちら👇
note: 今日の学び「スプレッドシートのデータをドキュメントにコピーしてみた」

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?