Class Document

ドキュメント

リッチテキストや表やリストなどの要素を含むドキュメント。

ドキュメントは、DocumentApp を使用して開いたり作成したりできます。

// Open a document by ID.
var doc = DocumentApp.openById("<my-id>");

// Create and open a document.
doc = DocumentApp.create("Document Title");

Methods

メソッド戻り値の型概要
addBookmark(position)Bookmark指定された PositionBookmark を追加します。
addEditor(emailAddress)DocumentDocument の編集者のリストに、指定されたユーザーを追加します。
addEditor(user)DocumentDocument の編集者のリストに、指定されたユーザーを追加します。
addEditors(emailAddresses)Document指定したユーザーの配列を Document の編集者のリストに追加します。
addFooter()FooterSectionドキュメント フッター セクションを追加します(存在しない場合)。
addHeader()HeaderSectionドキュメント ヘッダー セクションを追加します(存在しない場合)。
addNamedRange(name, range)NamedRangeNamedRange を追加しました。これは、後で取得するための名前と ID を持つ Range です。
addViewer(emailAddress)DocumentDocument の閲覧者リストに特定のユーザーを追加します。
addViewer(user)DocumentDocument の閲覧者リストに特定のユーザーを追加します。
addViewers(emailAddresses)Document指定したユーザーの配列を Document の閲覧者リストに追加します。
getAs(contentType)Blob指定された型の blob として、現在の Document コンテンツを取得します。
getBlob()Blob現在の Document コンテンツを blob として取得します。
getBody()Bodyアクティブ ドキュメントの Body を取得します。
getBookmark(id)Bookmark指定された ID の Bookmark を取得します。
getBookmarks()Bookmark[]ドキュメント内のすべての Bookmark オブジェクトを取得します。
getCursor()Positionアクティブ ドキュメント内のユーザーのカーソルを取得します。
getEditors()User[]この Document の編集者のリストを取得します。
getFooter()FooterSectionドキュメントのフッター セクションを取得します(存在する場合)。
getFootnotes()Footnote[]ドキュメント本文内のすべての Footnote 要素を取得します。
getHeader()HeaderSectionドキュメントのヘッダー セクションを取得します(存在する場合)。
getId()Stringドキュメントの一意の識別子を取得します。
getLanguage()Stringドキュメントの言語コードを取得します。
getName()Stringドキュメントのタイトルを取得します。
getNamedRangeById(id)NamedRange指定された ID の NamedRange を取得します。
getNamedRanges()NamedRange[]ドキュメント内のすべての NamedRange オブジェクトを取得します。
getNamedRanges(name)NamedRange[]指定された名前のドキュメント内のすべての NamedRange オブジェクトを取得します。
getSelection()Rangeアクティブ ドキュメント内のユーザーの選択を取得します。
getSupportedLanguageCodes()String[]Google ドキュメント ファイルでサポートされているすべての言語コードを取得します。
getUrl()String現在のドキュメントにアクセスするための URL を取得します。
getViewers()User[]この Document の閲覧者とコメント投稿者のリストを取得します。
newPosition(element, offset)Position新しい Position を作成します。これは、特定の要素を基準に、ドキュメント内の場所への参照です。
newRange()RangeBuilderドキュメント要素から Range オブジェクトを作成するために使用するビルダーを作成します。
removeEditor(emailAddress)DocumentDocument の編集者のリストから指定されたユーザーを削除します。
removeEditor(user)DocumentDocument の編集者のリストから指定されたユーザーを削除します。
removeViewer(emailAddress)DocumentDocument の閲覧者とコメント投稿者のリストから指定されたユーザーを削除します。
removeViewer(user)DocumentDocument の閲覧者とコメント投稿者のリストから指定されたユーザーを削除します。
saveAndClose()void現在の Document を保存します。
setCursor(position)DocumentPosition で指定されたアクティブ ドキュメント内のユーザーのカーソルを設定します。
setLanguage(languageCode)Documentドキュメントの言語コードを設定します。
setName(name)Documentドキュメントのタイトルを設定します。
setSelection(range)DocumentRange が設定された場合に、アクティブ ドキュメントでのユーザーの選択を設定します。

詳細なドキュメント

addBookmark(position)

指定された PositionBookmark を追加します。

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the document body and adds a paragraph.
const paragraph = doc.getBody().appendParagraph('My new paragraph.');

// Creates a position at the first character of the paragraph text.
const position = doc.newPosition(paragraph.getChild(0), 0);

// Adds a bookmark at the first character of the paragraph text.
const bookmark = doc.addBookmark(position);

// Logs the bookmark ID to the console.
console.log(bookmark.getId());

}

パラメータ

名前説明
positionPosition新しいブックマークの位置。

復路

Bookmark - 新しいブックマーク。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

addEditor(emailAddress)

Document の編集者のリストに、指定されたユーザーを追加します。ユーザーがすでに閲覧者のリストに含まれていた場合、このメソッドはユーザーを閲覧者リストから昇格させます。

パラメータ

名前説明
emailAddressString追加するユーザーのメールアドレス。

復路

Document - チェーン用の Document

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

addEditor(user)

Document の編集者のリストに、指定されたユーザーを追加します。ユーザーがすでに閲覧者のリストに含まれていた場合、このメソッドはユーザーを閲覧者リストから昇格させます。

パラメータ

名前説明
userUser追加するユーザーの表現。

復路

Document - チェーン用の Document

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

addEditors(emailAddresses)

指定したユーザーの配列を Document の編集者のリストに追加します。すでに閲覧者のリストに含まれていたユーザーがある場合、このメソッドはそのユーザーを視聴者のリストから昇格させます。

パラメータ

名前説明
emailAddressesString[]追加するユーザーのメールアドレスの配列。

復路

Document - チェーン用の Document

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

addFooter()

ドキュメント フッターのセクションを追加します(存在しない場合)。

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Adds a footer to the document.
const footer = doc.addFooter();

// Sets the footer text to 'This is a footer.'
footer.setText('This is a footer');

復路

FooterSection - ドキュメントのフッター。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

addHeader()

ドキュメント ヘッダー セクションを追加します(存在しない場合)。

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Adds a header to the document.
const header = doc.addHeader();

// Sets the header text to 'This is a header.'
header.setText('This is a header');

復路

HeaderSection - ドキュメント ヘッダー。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

addNamedRange(name, range)

NamedRange を追加しました。これは、後で取得するための名前と ID を持つ Range です。名前は一意であるとは限りません。HTML のクラスと同様に、同じドキュメント内の複数の範囲で同じ名前を使用できます。一方、ID は HTML の ID と同様に、ドキュメント内で一意です。NamedRange をドキュメントに追加すると、その内容は変更できず、削除のみ可能になります。

ドキュメントにアクセスするスクリプトはすべて NamedRange にアクセスできます。スクリプト間で意図しない競合が発生しないように、範囲名の前に一意の文字列を付加することを検討してください。

// Creates a named range that includes every table in the document.
var doc = DocumentApp.getActiveDocument();
var rangeBuilder = doc.newRange();
var tables = doc.getBody().getTables();
for (var i = 0; i < tables.length; i++) {
  rangeBuilder.addElement(tables[i]);
}
doc.addNamedRange('Document tables', rangeBuilder.build());

パラメータ

名前説明
nameString範囲の名前は一意である必要はなく、1 ~ 256 文字にする必要があります。
rangeRange名前に関連付ける要素の範囲。範囲は、有効な選択、検索結果、または newRange() を使用して手動で構築できます。

復路

NamedRange - NamedRange

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

addViewer(emailAddress)

Document の閲覧者リストに特定のユーザーを追加します。ユーザーがすでに編集者のリストに含まれていた場合、このメソッドは無視されます。

パラメータ

名前説明
emailAddressString追加するユーザーのメールアドレス。

復路

Document - チェーン用の Document

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

addViewer(user)

Document の閲覧者リストに特定のユーザーを追加します。ユーザーがすでに編集者のリストに含まれていた場合、このメソッドは無視されます。

パラメータ

名前説明
userUser追加するユーザーの表現。

復路

Document - チェーン用の Document

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

addViewers(emailAddresses)

指定したユーザーの配列を Document の閲覧者リストに追加します。すでに編集者のリストに含まれていたユーザーがある場合、このメソッドはそのユーザーに影響はありません。

パラメータ

名前説明
emailAddressesString[]追加するユーザーのメールアドレスの配列。

復路

Document - チェーン用の Document

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getAs(contentType)

指定された型の blob として、現在の Document コンテンツを取得します。

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the document as a PDF.
const pdf = doc.getAs('application/pdf');

// Logs the name of the PDF to the console.
console.log(pdf.getName());

パラメータ

名前説明
contentTypeString変換する MIME タイプ。現在のところ、'application/pdf' のみがサポートされています。

復路

Blob - blob としての現在のドキュメント。


getBlob()

現在の Document コンテンツを blob として取得します。

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Retrieves the current document's contents as a blob and logs it to the console.
console.log(doc.getBlob().getContentType());

復路

Blob - blob としての現在のドキュメント。


getBody()

有効なドキュメントの Body を取得します。

ドキュメントには、さまざまな種類のセクション(HeaderSectionFooterSection など)を含めることができます。ドキュメントの有効なセクションは Body です。

Document 内の要素メソッドはアクティブな Body にデリゲートします。

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the document body.
const body = doc.getBody();

// Gets the body text and logs it to the console.
console.log(body.getText());

復路

Body - アクティブなドキュメント本文のセクション。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getBookmark(id)

指定された ID の Bookmark を取得します。そのような Bookmark が存在しない場合、このメソッドは null を返します。

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the bookmark by its ID.
const bookmark = doc.getBookmark('id.xyz654321');

// If the bookmark exists, logs the character offset of its position to the console.
// otherwise, logs 'No bookmark exists with the given ID.' to the console.
if (bookmark) {
  console.log(bookmark.getPosition().getOffset());
} else {
  console.log('No bookmark exists with the given ID.');
}

パラメータ

名前説明
idStringBookmark の ID。

復路

Bookmark - 指定された ID を持つ Bookmark、またはそのような Bookmark が存在しない場合は null

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getBookmarks()

ドキュメント内のすべての Bookmark オブジェクトを取得します。

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
const doc = DocumentApp.openById('abc123456');

// Gets all of the bookmarks in the document.
const bookmarks = doc.getBookmarks();

// Logs the number of bookmarks in the document to the console.
console.log(bookmarks.length);

復路

Bookmark[] - ドキュメント内の Bookmark オブジェクトの配列。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getCursor()

アクティブ ドキュメント内のユーザーのカーソルを取得します。スクリプトがアクセスできるのは、そのスクリプトを実行しているユーザーのカーソルだけです。これは、スクリプトがドキュメントにバインドされている場合に限ります。

// Insert some text at the cursor position and make it bold.
var cursor = DocumentApp.getActiveDocument().getCursor();
if (cursor) {
  // Attempt to insert text at the cursor position. If the insertion returns null, the cursor's
  // containing element doesn't allow insertions, so show the user an error message.
  var element = cursor.insertText('ಠ‿ಠ');
  if (element) {
    element.setBold(true);
  } else {
    DocumentApp.getUi().alert('Cannot insert text here.');
  }
} else {
  DocumentApp.getUi().alert('Cannot find a cursor.');
}

復路

Position - ユーザーのカーソルの表現。null は、ドキュメント内にカーソルが置かれていない場合、またはスクリプトがドキュメントにバインドされていない場合に行われます。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getEditors()

この Document の編集者のリストを取得します。

復路

User[] - 編集権限を持つユーザーの配列。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getFooter()

ドキュメントのフッター セクション(存在する場合)を取得します。

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the text of the document's footer and logs it to the console.
console.log(doc.getFooter().getText());

復路

FooterSection - ドキュメントのフッター。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getFootnotes()

ドキュメント本文内のすべての Footnote 要素を取得します。

getFootnotes を呼び出すと、ドキュメントの要素が反復処理されます。サイズの大きいドキュメントの場合は、このメソッドを不必要に呼び出すことは避けてください。

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the first footnote.
const footnote = doc.getFootnotes()[0];

// Logs footnote contents to the console.
console.log(footnote.getFootnoteContents().getText());

復路

Footnote[] - ドキュメントの脚注。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getHeader()

ドキュメントのヘッダー セクションを取得します(存在する場合)。

// Opens the Docs file by its ID. If you created your script from within
// a Google Docs file, you can use DocumentApp.getActiveDocument() instead.
// TODO(developer): Replace the ID with your own.
const doc = DocumentApp.openById('abc123456');

// Gets the text of the document's header and logs it to the console.
console.log(doc.getHeader().getText());

復路

HeaderSection - ドキュメント ヘッダー。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getId()

ドキュメントの一意の識別子を取得します。ドキュメント ID は、DocumentApp.openById() で特定のドキュメント インスタンスを開くために使用されます。

復路

String - ドキュメントの ID

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getLanguage()

ドキュメントの言語コードを取得します。これはドキュメント エディタの [File] > [Language] に表示される言語であり、ドキュメント内の実際の言語とは異なる場合があります。

復路

String - ドキュメントの言語、または定義されていない場合は null

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getName()

ドキュメントのタイトルを取得します。

復路

String - ドキュメント タイトル

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getNamedRangeById(id)

指定された ID の NamedRange を取得します。そのような NamedRange が存在しない場合、このメソッドは null を返します。名前は必ずしも一意ではありません。HTML のクラスと同様に、同じドキュメント内の異なる範囲には同じ名前を使用できます。一方、ID は HTML の ID と同様に、ドキュメント内で一意です。

パラメータ

名前説明
idString範囲の ID(ドキュメント内で一意)

復路

NamedRange - 指定された ID を持つ NamedRange、またはそのような範囲が存在しない場合は null

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getNamedRanges()

ドキュメント内のすべての NamedRange オブジェクトを取得します。

NamedRange には、ドキュメントにアクセスするスクリプトからアクセスできます。スクリプト間で意図しない競合が発生しないように、範囲名の前に一意の文字列を付加することを検討してください。

復路

NamedRange[] - ドキュメント内の NamedRange オブジェクトの配列。同じ名前の複数の範囲が含まれている場合があります。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getNamedRanges(name)

指定された名前のドキュメント内のすべての NamedRange オブジェクトを取得します。名前は一意ではなく、HTML のクラスと同じように、同じドキュメント内の異なる範囲が同じ名前を持つ場合があります。一方、ID は HTML の ID と同様に、ドキュメント内で一意です。

NamedRange には、ドキュメントにアクセスするスクリプトからアクセスできます。スクリプト間で意図しない競合が発生しないように、範囲名の前に一意の文字列を付加することを検討してください。

パラメータ

名前説明
nameString範囲の名前。一意とは限らない

復路

NamedRange[] - 指定した名前のドキュメント内の NamedRange オブジェクトの配列

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getSelection()

アクティブ ドキュメント内のユーザーの選択を取得します。スクリプトがアクセスできるのは、スクリプトを実行しているユーザーの選択と、スクリプトがドキュメントにバインドされている場合のみです。

// Display a dialog box that tells the user how many elements are included in the selection.
var selection = DocumentApp.getActiveDocument().getSelection();
if (selection) {
  var elements = selection.getRangeElements();
  DocumentApp.getUi().alert('Number of selected elements: ' + elements.length);
} else {
  DocumentApp.getUi().alert('Nothing is selected.');
}

復路

Range - ユーザーの選択の表現。ユーザーがドキュメント内で選択していないものや、段落の終わりだけが選択されている場合、段落の終わりと新しい行のみが選択された場合、またはスクリプトがドキュメントにバインドされていない場合には、null となります。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getSupportedLanguageCodes()

Google ドキュメント ファイルでサポートされているすべての言語コードを取得します。

復路

String[] - 言語コードの配列。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getUrl()

現在のドキュメントにアクセスするための URL を取得します。

var doc = DocumentApp.getActiveDocument();

// Send out the link to open the document.
MailApp.sendEmail("<email-address>", doc.getName(), doc.getUrl());

復路

String - 現在のドキュメントにアクセスするための URL

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

getViewers()

この Document の閲覧者とコメント投稿者のリストを取得します。

復路

User[] - 表示またはコメントの権限を持つユーザーの配列。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

newPosition(element, offset)

新しい Position を作成します。これは、特定の要素を基準に、ドキュメント内の場所への参照です。ユーザーのカーソルは、特に Position として表されます。

// Append a paragraph, then place the user's cursor after the first word of the new paragraph.
var doc = DocumentApp.getActiveDocument();
var paragraph = doc.getBody().appendParagraph('My new paragraph.');
var position = doc.newPosition(paragraph.getChild(0), 2);
doc.setCursor(position);

パラメータ

名前説明
elementElement新しい Position を格納する要素。Text 要素か、Paragraph などのコンテナ要素のいずれかにする必要があります。
offsetIntegerText 要素の場合は、Position の前の文字数。他の要素の場合は、同じコンテナ要素内の Position の前の子要素の数。

復路

Position - 新しい Position

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

newRange()

ドキュメント要素から Range オブジェクトを作成するために使用するビルダーを作成します。

// Change the user's selection to a range that includes every table in the document.
var doc = DocumentApp.getActiveDocument();
var rangeBuilder = doc.newRange();
var tables = doc.getBody().getTables();
for (var i = 0; i < tables.length; i++) {
  rangeBuilder.addElement(tables[i]);
}
doc.setSelection(rangeBuilder.build());

復路

RangeBuilder - 新しいビルダー

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

removeEditor(emailAddress)

Document の編集者のリストから指定されたユーザーを削除します。このメソッドは、Document がユーザーのドメイン全体と共有されている場合や、Document がアクセスできる共有ドライブにある場合など、一般的なアクセス権を持つユーザーのクラスに属している場合、Document へのアクセスをブロックしません。

ドライブ内のファイルの場合、閲覧者のリストからユーザーが削除されます。

パラメータ

名前説明
emailAddressString削除するユーザーのメールアドレス。

復路

Document - チェーン用の Document

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

removeEditor(user)

Document の編集者のリストから指定されたユーザーを削除します。このメソッドは、Document がユーザーのドメイン全体と共有されている場合や、Document がアクセスできる共有ドライブにある場合など、一般的なアクセス権を持つユーザーのクラスに属している場合、Document へのアクセスをブロックしません。

ドライブ内のファイルの場合、閲覧者のリストからユーザーが削除されます。

パラメータ

名前説明
userUser削除するユーザーの表現。

復路

Document - チェーン用の Document

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

removeViewer(emailAddress)

Document の閲覧者とコメント投稿者のリストから指定されたユーザーを削除します。ユーザーが閲覧者であれ、コメント投稿者でもありません。この方法では、Document がユーザーのドメイン全体と共有されている場合、または Document がアクセスできる共有ドライブにある場合など、一般的なアクセス権を持つユーザーのクラスに属している場合でも、Document へのアクセスはブロックされません。

ドライブ ファイルの場合は、編集者のリストからユーザーが削除されます。

パラメータ

名前説明
emailAddressString削除するユーザーのメールアドレス。

復路

Document - チェーン用の Document

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

removeViewer(user)

Document の閲覧者とコメント投稿者のリストから指定されたユーザーを削除します。ユーザーが閲覧者ではなく編集者の場合、このメソッドは何の効果も生じさせません。このメソッドは、Document がユーザーのドメイン全体と共有されている場合や、Document がアクセスできる共有ドライブにある場合など、一般的なアクセス権を持つユーザーのクラスに属している場合、Document へのアクセスをブロックしません。

ドライブ ファイルの場合は、編集者のリストからユーザーが削除されます。

パラメータ

名前説明
userUser削除するユーザーの表現。

復路

Document - チェーン用の Document

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

saveAndClose()

現在の Document を保存します。保留中の更新がフラッシュされて適用されます。

saveAndClose() メソッドは、スクリプト実行の終了時に、編集可能な Document ごとに自動的に呼び出されます。

閉じた Document は編集できません。DocumentApp.openById() を使用して、特定のドキュメントを編集用に再度開きます。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setCursor(position)

Position で指定されたアクティブ ドキュメント内のユーザーのカーソルを設定します。スクリプトがアクセスできるのは、そのスクリプトを実行しているユーザーのカーソルだけです。これは、スクリプトがドキュメントにバインドされている場合に限ります。

// Append a paragraph, then place the user's cursor after the first word of the new paragraph.
var doc = DocumentApp.getActiveDocument();
var paragraph = doc.getBody().appendParagraph('My new paragraph.');
var position = doc.newPosition(paragraph.getChild(0), 2);
doc.setCursor(position);

パラメータ

名前説明
positionPosition新しいカーソルの場所

復路

Document - この Document(チェーン用)

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setLanguage(languageCode)

ドキュメントの言語コードを設定します。これはドキュメント エディタの [File] > [Language] に表示される言語であり、ドキュメント内の実際の言語とは異なる場合があります。すべての有効な言語コードを取得するには getSupportedLanguageCodes() を使用します。

パラメータ

名前説明
languageCodeString言語コード。

復路

Document - チェーン用の Document

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setName(name)

ドキュメントのタイトルを設定します。

パラメータ

名前説明
nameString新しいドキュメントのタイトル

復路

Document - 現在のドキュメント

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents

setSelection(range)

Range が設定された場合に、アクティブ ドキュメントでのユーザーの選択を設定します。スクリプトがアクセスできるのは、スクリプトを実行しているユーザーの選択と、スクリプトがドキュメントにバインドされている場合のみです。

// Change the user's selection to a range that includes every table in the document.
var doc = DocumentApp.getActiveDocument();
var rangeBuilder = doc.newRange();
var tables = doc.getBody().getTables();
for (var i = 0; i < tables.length; i++) {
  rangeBuilder.addElement(tables[i]);
}
doc.setSelection(rangeBuilder.build());

パラメータ

名前説明
rangeRange選択する要素の新しい範囲

復路

Document - この Document(チェーン用)

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。

  • https://www.googleapis.com/auth/documents.currentonly
  • https://www.googleapis.com/auth/documents