Last active
October 20, 2020 09:39
-
-
Save hisasann/ad3d835317ed95db5a8966265d9c12e6 to your computer and use it in GitHub Desktop.
Google Document でテンプレートとなる docs からひな形をコピーする Google Apps Script
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
// Google Apps Scriptで議事録テンプレ作成を楽にした - Qiita - https://qiita.com/wiroha/items/5ce99d7bfc56e3270be6 | |
function onOpen() { | |
// Google Apps Scriptを使った独自メニューの作り方 - Qiita - https://qiita.com/howdy39/items/46ca1f2fd9d27eaba0c3 | |
const ui = DocumentApp.getUi(); // Uiクラスを取得する | |
const menu = ui.createMenu('日報'); // Uiクラスからメニューを作成する | |
menu.addItem('1日分を新たに作成', 'insertTemplate'); // メニューにアイテムを追加する | |
menu.addToUi(); // メニューをUiクラスに追加する | |
} | |
function insertTemplate() { | |
// URLの一部がID https://docs.google.com/document/d/XXXXXXXXXXXXXXXXXXXXXX/edit | |
const templateDoc_ID = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXX'; | |
// テンプレート用の docs | |
const templateDoc = DocumentApp.openById(templateDoc_ID); | |
// テンプレート の body のテキストたち(パラグラフ) | |
const templateParagraphs = templateDoc.getBody().getParagraphs(); | |
// この docs | |
const thisDoc = DocumentApp.getActiveDocument(); | |
// この docs の body | |
const thisBody = thisDoc.getBody(); | |
// この docs の body のテキストたち(パラグラフ) | |
const thisParagraphs = thisBody.getParagraphs(); | |
const INSERT_TEXT = 'ここの下にテンプレートを挿入します(この行は消さないでね)'; | |
// 挿入位置を決めるための場所を文字列がから探し出す | |
let child; | |
for (let i = 0; i < thisParagraphs.length; i++) { | |
let paragraph = thisParagraphs[i]; | |
let index = i; | |
let text = paragraph.getText(); | |
if (text === INSERT_TEXT) { | |
Logger.log('index %s text %s', index, text); | |
child = paragraph; | |
break; | |
} | |
} | |
let childIndex = thisBody.getChildIndex(child); | |
// 挿入位置の下に挿入したいのでインクリメントする | |
childIndex++; | |
Logger.log('childIndex %s', childIndex); | |
// テンプレートのテキストをコピーする | |
templateDoc.getBody().getParagraphs().forEach(function (value, i) { | |
console.log(value, i); | |
if (value.getType() === DocumentApp.ElementType.LIST_ITEM) { // 箇条書きの場合 | |
thisBody.insertListItem(i + childIndex, value.getText()).setGlyphType(DocumentApp.GlyphType.BULLET); | |
} else { | |
thisBody.insertParagraph(i + childIndex, value.getText()); | |
} | |
}); | |
// templateDoc.getBody().getParagraphs() が激重なので、なるべく少ない回数で呼びたいためループを分けた | |
const newThisParagraphs = thisBody.getParagraphs(); | |
templateDoc.getBody().getParagraphs().forEach(function (value, i) { | |
if (value.getType() !== DocumentApp.ElementType.LIST_ITEM) { | |
newThisParagraphs[i + childIndex].setAttributes(value.getAttributes()); // 見出しなどの属性を設定 | |
} | |
}); | |
// 横棒区切り線を入れる | |
// Class Body | Apps Script | Google Developers - https://developers.google.com/apps-script/reference/document/body#inserthorizontalrulechildindex | |
thisDoc.getBody().insertHorizontalRule(childIndex + templateParagraphs.length); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment