Feishu Development Study Notes (2) - Simple Cloud Document Development Practice

Feishu Development Study Notes (2) - Simple Cloud Document Development Practice

1. Cloud Document Feishu Development Environment API

First of all, enter the open platform
Feishu open platform:https://open.feishu.cn/app?lang=zh-CN
Cloud document related APIs are in the "Cloud Document" directory, under which there are sub-modules such as "Cloud Space", "Document", "Spreadsheet", "Multidimensional Table", etc.
Each has its corresponding functional API.
Insert image description here

2. Use Feishu API to operate folders and cloud documents

2.1 Get the file list in my space directory

For an enterprise user, he has a "personal space" directory, which corresponds to the "my space" root folder.
Insert image description here
The API to obtain the file list in the root directory of my space is as follows:

真实请求地址: 
GET
https://open.feishu.cn/open-apis/drive/v1/files?direction=DESC&order_by=EditedTime

The return result is a Json string, consisting of a response header and a response body, where the response body is the key return information, as follows:

{
    
    
  "code": 0,
  "data": {
    
    
    "files": [],
    "has_more": false
  },
  "msg": "success"
}

You can see that the files:[] array is empty, that is, there are no files in the home directory.
For testing, we create a "test document" cloud document and a "test form" spreadsheet under the "My Space" directory, as well as a "test sub- Directory", and then query again

Insert image description here

The response body result returned is as follows:
code 0 means the query is successful
The files[] array is no longer empty, but has three elements. , corresponding to "test subdirectory", "test form", and "test document"

{
    
    
  "code": 0,
  "data": {
    
    
    "files": [
      {
    
    
        "created_time": "1691555",
        "modified_time": "1691555",
        "name": "测试子目录",
        "owner_id": "ou_3bd0cc",
        "parent_token": "nodcnfcMb",
        "token": "VGOkfIDnuf",
        "type": "folder",
        "url": "https://test-cptojg6atdfe.feishu.cn/drive/folder/VGOkrUnuf"
      },
      {
    
    
        "created_time": "16996",
        "modified_time": "169682",
        "name": "测试表格",
        "owner_id": "ou_3bd053a2cc",
        "parent_token": "nodcnEBAfcMb",
        "token": "SVT0Zh",
        "type": "sheet",
        "url": "https://test-cptojg6atdfe.feishu.cn/sheets/SVT0Rnhh"
      },
      {
    
    
        "created_time": "1699408",
        "modified_time": "1692455",
        "name": "测试文档",
        "owner_id": "ou_3bd053cc",
        "parent_token": "nodcnEcMb",
        "token": "PAiZdu4wuAe",
        "type": "docx",
        "url": "https://test-cptojg6atdfe.feishu.cn/docx/PAiZdunAe"
      }
    ],
    "has_more": false
  },
  "msg": "success"
}

Each element in files consists of 8 fields:
created_time: Creation timestamp
modified_time: Modification timestamp
name: document name
owner_id: document owner ID
parent_token: token of the parent object, here is "My Space" The token of the directory, you can use this token to get the parent object folder
token: the token of this document, which is the "ID card" of the current document, you can also use this command Get this document
type: Here you can see that the folder type is folder, the spreadsheet type is sheet, and the document type is docx
url : The https link address of the current file, through this addressjump access.

2.2 Get file data

The file here is of uncertain type, it may be doc, docx or sheet
With the token of the file, you can use it to access the metadata of the document< /span>
Insert image description here
Return data

{
    
    
  "code": 0,
  "msg": "success",
  "data": {
    
    
    "metas": [
      {
    
    
        "doc_token": "doccnfbcef",
        "doc_type": "doc",
        "title": "测试文档",
        "owner_id": "ou_b13d41c02edc5f1abcef",
        "create_time": "1652045",
        "latest_modify_user": "ou_b13d1abcef",
        "latest_modify_time": "16520",
        "url": "https://sample.feishu.cn/docs/doccabcef",
        "sec_label_name": "L2-内部"
      }
    ],
    "failed_list": [
      {
    
    
        "token": "boxcachabcef",
        "code": 970005
      }
    ]
  }
}

Among them, code 0 is returned successfully
The basic information of the document is returned in the metas list.
sec_label_name is the name of the confidentiality level.
There are also API functions for copy/move/delete operations. The core parameters are to get the token of the operation directory and the current The document’s token.

2.3 Obtain detailed content of cloud documents

  1. Get basic information about cloud documents

According to the document_id of the document, and with the API permissions for related document operations enabled, you can use the API under Cloud Space->Document->New Version Document to obtain the entire content of the document.
The document content is composed of several blocks, so after obtaining the document, you need to obtain the information of the blocks and sub-blocks.
document_id is the unique id of the document, which is the last part of the document's address. Currently, it is found that it is consistent with the token of the current document.
Insert image description here
To obtain the basic information of the document, you need to fill in document_id
Insert image description here
Return information:
Only revision_id version id and title

{
    
    
  "code": 0,
  "data": {
    
    
    "document": {
    
    
      "document_id": "PAiZdxxxx",
      "revision_id": 7,
      "title": "测试文档"
    }
  },
  "msg": "success"
}
  1. Get the plain text content of the cloud document
    Insert image description here
    Return data
{
    
    
  "code": 0,
  "data": {
    
    
    "content": "测试文档\n测试文档\n"
  },
  "msg": "success"
}

Content returns plain text content, including newline characters and other characters.

  1. Get all blocks of cloud document
    Insert image description here
    Return data
{
    
    
  "code": 0,
  "data": {
    
    
    "has_more": false,
    "items": [
      {
    
    
        "block_id": "PAiZdu4wuoNPxxxxx",
        "block_type": 1,
        "children": [
          "Vvabd8vG3ozusxxx"
        ],
        "page": {
    
    
          "elements": [
            {
    
    
              "text_run": {
    
    
                "content": "测试文档",
                "text_element_style": {
    
    
                  "bold": false,
                  "inline_code": false,
                  "italic": false,
                  "strikethrough": false,
                  "underline": false
                }
              }
            }
          ],
          "style": {
    
    
            "align": 1
          }
        },
        "parent_id": ""
      },
      {
    
    
        "block_id": "Vvabd8vGxxxx",
        "block_type": 5,
        "heading3": {
    
    
          "elements": [
            {
    
    
              "text_run": {
    
    
                "content": "测试文档",
                "text_element_style": {
    
    
                  "bold": false,
                  "inline_code": false,
                  "italic": false,
                  "strikethrough": false,
                  "underline": false
                }
              }
            }
          ],
          "style": {
    
    
            "align": 1,
            "folded": false
          }
        },
        "parent_id": "PAiZdu4wuoNPxxxxxxxx"
      }
    ]
  },
  "msg": "success"
}

As you can see from the returned data, there is block information in the items under data.
The main fields of block are
block_id is the id of the block
block_type is the type of the block
children is the id list of sub-blocks
Next is the type name of the block, for example, page is the text, heading3 is heading 3, and the list after the name is the format and content of the sub-elements< /span> As above, the entire content of the cloud document is obtained. The last information of the child block is the id of the parent element, which is parent_id text_element_style: style of text element, including bold, underline, strikethrough, etc. etc. content: text content
text_run: sub-element type



2.4 Get the details of the spreadsheet

  1. Get spreadsheet and worksheet information
    Similar to cloud documents, you can get table information through another spreadsheet password spreadsheet_token
    Insert image description here

returned messages

{
    
    
  "code": 0,
  "data": {
    
    
    "spreadsheet": {
    
    
      "owner_id": "ou_3bda2cc",
      "title": "测试表格",
      "token": "SVTxxxx",
      "url": "https://test-cptojg6atdfe.feishu.cn/sheets/SVhh"
    }
  },
  "msg": ""
}

Returned owner_id, title, token, url
Get detailed information of the worksheet
Insert image description here

returned messages

{
    
    
  "code": 0,
  "data": {
    
    
    "sheets": [
      {
    
    
        "grid_properties": {
    
    
          "column_count": 20,
          "frozen_column_count": 0,
          "frozen_row_count": 0,
          "row_count": 200
        },
        "hidden": false,
        "index": 0,
        "resource_type": "sheet",
        "sheet_id": "885d89",
        "title": "Sheet1"
      },
      {
    
    
        "grid_properties": {
    
    
          "column_count": 20,
          "frozen_column_count": 0,
          "frozen_row_count": 0,
          "row_count": 200
        },
        "hidden": false,
        "index": 1,
        "resource_type": "sheet",
        "sheet_id": "xBuAyL",
        "title": "Sheet2"
      }
    ]
  },
  "msg": ""
}

There are 2 elements in the returned information body sheets list, so two worksheets are queried
Sheet1 id:885d89
Sheet2 id: xBuAyL
grid_properties contains row and column number information, as well as frozen row and column number information

  1. First use spreadsheet_token and sheet_id to query worksheet information
    Insert image description here
    Return data
{
    
    
  "code": 0,
  "data": {
    
    
    "sheet": {
    
    
      "grid_properties": {
    
    
        "column_count": 20,
        "frozen_column_count": 0,
        "frozen_row_count": 0,
        "row_count": 200
      },
      "hidden": false,
      "index": 0,
      "resource_type": "sheet",
      "sheet_id": "885d89",
      "title": "Sheet1"
    }
  },
  "msg": ""
}

Similar to obtaining spreadsheet information, it specifies worksheet information and the content is relatively simple.

  1. Get specific cell and value information in the table based on the worksheet id
    The debugging toolbench does not support API debugging, but back to the development documentation, the server API is introduced. Therefore, we can only use API capabilities to read cell and value information by developing programs.
    4.
    Insert image description here
    This will be tested in the development environment next time.

Guess you like

Origin blog.csdn.net/qq_43662503/article/details/134224834