タブの操作

Google Docs API を使用すると、ドキュメント内のどのタブからでもコンテンツにアクセスできます。

タブとは

Google ドキュメントには、「タブ」と呼ばれる組織レイヤがあります。 ドキュメントでは、ユーザーは単一のタブ内に 1 つ以上のタブを作成できます。 現在のスプレッドシートのタブとほぼ同じです。各タブには title と ID(URL に追加されます)。また、タブには子タブを設定できます。 他のタブの下にネストされたタブ

子タブの API サポートは現在利用できますが、UI サポートは近日中に提供される予定です。 UI サポートがリリースされたタイミングで子タブをコード内で処理できます。 これ以上コードを更新する必要はありません

ドキュメント リソースでのドキュメント コンテンツの表現方法の構造的な変更

以前のドキュメントにはタブの概念がなかったため、 Document 直接含まれているリソース すべてのテキスト コンテンツを次のフィールドに入力します。

タブの構造的階層が追加されたため、これらのフィールドは ドキュメント内のすべてのタブのテキスト コンテンツを意味的に表現します。「 テキストベースのコンテンツが別のレイヤで表現されるようになりました。タブのプロパティと Google ドキュメント内のコンテンツに、 document.tabs: Tab 個のオブジェクト(それぞれが には、前述のテキスト コンテンツ フィールドがすべて含まれます。以降のセクションでは 簡単な概要 タブの JSON 表現 より詳細な情報が表示されます

タブのプロパティにアクセスする

タブのプロパティにアクセスするには tab.tabProperties これには、タブの ID、タイトル、位置などの情報が含まれます。

タブ内のテキスト コンテンツにアクセスする

タブ内の実際のドキュメント コンテンツは、 tab.documentTab。次のすべて 上記のテキスト コンテンツ フィールドには、tab.documentTab を使用してアクセスできます。 たとえば、document.body ではなく、次のコマンドを使用します。 document.tabs[indexOfTab].documentTab.body

タブ階層

子タブは API では tab.childTabs フィールドがオン Tab。クラスタ内のすべてのタブにアクセスすると、 「ツリー」を走査する必要があるため、子タブのリストを返します。たとえば、 次のようなタブ階層を含むドキュメントです。

3 つの最上位タブ(一部には子タブがある)を含むタブリスト UI

Body を取得するには タブ 3.1.2 から document.tabs[2].childTabs[0].childTabs[1].documentTab.body。サンプルを見る 後のセクションで説明します。ここには、反復処理を行うサンプルコードが ドキュメントのすべてのタブに対して 同じ操作を行えます

メソッドの変更

タブの導入により、各ドキュメント メソッドにいくつかの変更が加えられました コードの更新が必要になる場合があります

documents.get

デフォルトでは、すべてのタブの内容が返されるわけではありません。デベロッパーは すべてのタブにアクセスできます。「 documents.get メソッドは includeTabsContent パラメータ。このパラメータを使用すると、 すべてのタブがレスポンスに含まれます。

  • includeTabsContenttrue に設定されている場合、 documents.get メソッドが返す Document リソースに document.tabs フィールドに値が入力されています。すべてのテキスト フィールドが document に直接追加されている (例: document.body)は空のままになります。
  • includeTabsContent が指定されていない場合、Document リソースのテキスト フィールド(例: document.body)には、最初のタブのコンテンツのみが入力されます。document.tabs フィールドは空になり、他のタブのコンテンツは返されません。

documents.create

documents.create メソッド Document リソースを返します。 作成された空のドキュメントを表します。返される Document リソースは ドキュメントのテキスト コンテンツ フィールドと空のドキュメント コンテンツの両方に document.tabs

document.batchUpdate

Request に含まれるもの 更新を適用するタブを指定する手段です。デフォルトでは 場合、 Requestは、 ドキュメントの最初のタブに適用されます。 ReplaceAllTextRequest DeleteNamedRangeRequest, および ReplaceNamedRangeContentRequest 3 つの特別なリクエストで、デフォルトですべてのタブに適用されます。

詳しくは、 Request 秒 ドキュメントをご覧ください

ドキュメント内のタブ、ブックマーク、見出しへの内部リンクを作成できます。 タブ機能の導入により、link.bookmarkId と 次のフィールドの link.headingId フィールド: Link リソースは削除できなくなりました ドキュメントの特定のタブにあるブックマークや見出しを表します。

デベロッパーは、link.bookmark を使用するようにコードを更新する必要があります。 読み取り / 書き込みオペレーションでの link.heading。外部リンクは BookmarkLinkHeadingLink 個のオブジェクト( ブックマークまたは見出しの ID と配置されているタブの ID を含む できます。また、link.tabId はタブへの内部リンクを公開します。

documents.get のコンテンツをリンクする レスポンスは、includeTabsContent パラメータによっても異なります。

  • includeTabsContenttrue に設定すると、すべての内部リンクが公開されます (link.bookmarklink.heading など)。以前のフィールドは使用されなくなります。
  • includeTabsContent が指定されていない場合、 単一のタブ、その単一のタブ内のブックマークや見出しへの内部リンク 引き続き link.bookmarkIdlink.headingId として公開されます。ドキュメント内 複数のタブを含む場合、内部リンクは link.bookmark として公開されます。 link.heading

document.batchUpdate 内、 以前のフィールド、ブックマーク、または 見出しは、 Request。どのタブも ドキュメント内の最初のタブのものとみなされます。

Link JSON 表現では、 より詳細な情報が表示されます

タブの一般的な使用パターン

次のコードサンプルでは、タブを操作するさまざまな方法を示しています。

ドキュメント内のすべてのタブのタブ コンテンツを読み上げる

タブ機能の前にこの操作を行っていた既存のコードを、サポートに移行できる includeTabsContent パラメータを true に設定してタブを開き、 タブツリー階層を作成し、ビューからゲッター メソッドを呼び出す TabDocumentTab: Document を選択します。次の部分的 コードサンプルは ドキュメントからテキストを抽出する。内容 ドキュメントの各タブのすべてのテキスト コンテンツを印刷する方法を示します。このタブ トラバーサル コードは、インフラストラクチャの維持や管理を気にしない他の多くの タブの実際の構造を確認できます。

Java

/** Prints all text contents from all tabs in the document. */
static void printAllText(Docs service, String documentId) throws IOException {
  // Fetch the document with all of the tabs populated, including any nested
  // child tabs.
  Document doc =
      service.documents().get(documentId).setIncludeTabsContent(true).execute();
  List<Tab> allTabs = getAllTabs(doc);

  // Print the content from each tab in the document.
  for (Tab tab: allTabs) {
    // Get the DocumentTab from the generic Tab.
    DocumentTab documentTab = tab.getDocumentTab();
    System.out.println(
        readStructuralElements(documentTab.getBody().getContent()));
  }
}

/**
 * Returns a flat list of all tabs in the document in the order they would
 * appear in the UI (top-down ordering). Includes all child tabs.
 */
private List<Tab> getAllTabs(Document doc) {
  List<Tab> allTabs = new ArrayList<>();
  // Iterate over all tabs and recursively add any child tabs to generate a
  // flat list of Tabs.
  for (Tab tab: doc.getTabs()) {
    addCurrentAndChildTabs(tab, allTabs);
  }
  return allTabs;
}

/**
 * Adds the provided tab to the list of all tabs, and recurses through and
 * adds all child tabs.
 */
private void addCurrentAndChildTabs(Tab tab, List<Tab> allTabs) {
  allTabs.add(tab);
  for (Tab tab: tab.getChildTabs()) {
    addCurrentAndChildTabs(tab, allTabs);
  }
}

/**
 * Recurses through a list of Structural Elements to read a document's text
 * where text may be in nested elements.
 *
 * <p>For a code sample, see
 * <a href="https://developers.google.com/docs/api/samples/extract-text">Extract
 * the text from a document</a>.
 */
private static String readStructuralElements(List<StructuralElement> elements) {
  ...
}

ドキュメントの最初のタブからタブの内容を読み上げる

これは、すべてのタブを読み取る場合と同様です。

Java

/** Prints all text contents from the first tab in the document. */
static void printAllText(Docs service, String documentId) throws IOException {
  // Fetch the document with all of the tabs populated, including any nested
  // child tabs.
  Document doc =
      service.documents().get(documentId).setIncludeTabsContent(true).execute();
  List<Tab> allTabs = getAllTabs(doc);

  // Print the content from the first tab in the document.
  Tab firstTab = allTabs.get(0);
  // Get the DocumentTab from the generic Tab.
  DocumentTab documentTab = firstTab.getDocumentTab();
  System.out.println(
      readStructuralElements(documentTab.getBody().getContent()));
}

最初のタブの更新をリクエストする

次のコードサンプルの一部は、Google Chat で特定のタブをターゲットにする方法を示しています。 Request。このコード サンプルに基づいていますが テキストの挿入、削除、移動のガイド。

Java

/** Inserts text into the first tab of the document. */
static void insertTextInFirstTab(Docs service, String documentId)
    throws IOException {
  // Get the first tab's ID.
  Document doc =
      service.documents().get(documentId).setIncludeTabsContent(true).execute();
  Tab firstTab = doc.getTabs().get(0);
  String tabId = firstTab.getTabProperties().getTabId();

  List<Request>requests = new ArrayList<>();
  requests.add(new Request().setInsertText(
      new InsertTextRequest().setText(text).setLocation(new Location()
                                                            // Set the tab ID.
                                                            .setTabId(tabId)
                                                            .setIndex(25))));

  BatchUpdateDocumentRequest body =
      new BatchUpdateDocumentRequest().setRequests(requests);
  BatchUpdateDocumentResponse response =
      docsService.documents().batchUpdate(DOCUMENT_ID, body).execute();
}