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)Document指定されたユーザーを Document の編集者のリストに追加します。
addEditor(user)Document指定されたユーザーを Document の編集者のリストに追加します。
addEditors(emailAddresses)Document指定されたユーザー配列を Document の編集者のリストに追加します。
addFooter()FooterSectionドキュメントのフッター セクションを追加します(存在しない場合)。
addHeader()HeaderSectionドキュメント ヘッダー セクションを追加します(存在しない場合)。
addNamedRange(name, range)NamedRangeNamedRange を追加します。これは、後で取得に使用する名前と ID を持つ Range です。
addViewer(emailAddress)Document指定されたユーザーを Document の閲覧者リストに追加します。
addViewer(user)Document指定されたユーザーを Document の閲覧者リストに追加します。
addViewers(emailAddresses)Document指定されたユーザー配列を Document の閲覧者リストに追加します。
getAs(contentType)Blob現在の Document コンテンツを指定された型の blob として取得します。
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)

現在の 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');

// 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()

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

リターン

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)

ドキュメントの言語コードを設定します。これは、ドキュメント エディタの [ファイル] > [言語] に表示される言語であり、ドキュメントに含まれている実際の言語とは異なる場合があります。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