首页
/ Cheerio for Google Apps Script 使用教程

Cheerio for Google Apps Script 使用教程

2024-08-25 23:32:33作者:仰钰奇

1. 项目的目录结构及介绍

Cheerio for Google Apps Script 项目的目录结构如下:

cheeriogs/
├── src/
│   └── index.js
├── package.json
├── build.js
└── README.md

目录结构介绍

  • src/: 包含项目的主要源代码文件。
    • index.js: 项目的入口文件。
  • package.json: 项目的配置文件,包含项目的基本信息、依赖和脚本命令。
  • build.js: 用于构建项目的脚本文件。
  • README.md: 项目的说明文档。

2. 项目的启动文件介绍

项目的启动文件是 src/index.js。这个文件是项目的入口点,负责加载和初始化 Cheerio 库。

// src/index.js
// 示例代码,实际内容可能有所不同
const content = getContent_('https://en.wikipedia.org');
const $ = Cheerio.load(content);
Logger.log($('#mp-right').text());

启动文件功能

  • 加载网页内容: 使用 getContent_ 函数获取指定 URL 的内容。
  • 初始化 Cheerio: 使用 Cheerio.load 加载获取的内容。
  • 解析和操作 DOM: 使用 Cheerio 选择器操作 DOM 元素。

3. 项目的配置文件介绍

项目的配置文件是 package.json。这个文件包含了项目的基本信息、依赖和脚本命令。

{
  "name": "cheeriogs",
  "version": "1.0.0",
  "description": "Cheerio for Google Apps Script",
  "main": "src/index.js",
  "scripts": {
    "build": "node build.js",
    "deploy": "clasp push",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/tani/cheeriogs.git"
  },
  "keywords": [
    "google",
    "gas"
  ],
  "author": "TANIGUCHI Masaya",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/tani/cheeriogs/issues"
  },
  "homepage": "https://github.com/tani/cheeriogs#readme",
  "dependencies": {
    "cheerio": "^1.0.0"
  },
  "devDependencies": {
    "@google/clasp": "2.4.2",
    "esbuild": "^0.23.0",
    "esbuild-gas-plugin": "^0.8.0"
  }
}

配置文件内容介绍

  • name: 项目名称。
  • version: 项目版本。
  • description: 项目描述。
  • main: 项目的主入口文件。
  • scripts: 包含可执行的脚本命令。
    • build: 构建项目的命令。
    • deploy: 部署项目的命令。
    • test: 测试项目的命令。
  • repository: 项目的仓库信息。
  • keywords: 项目的关键词。
  • author: 项目作者。
  • license: 项目许可证。
  • bugs: 项目问题追踪地址。
  • homepage: 项目主页。
  • dependencies: 项目依赖的库。
  • devDependencies: 开发环境依赖的库。

以上是 Cheerio for Google Apps Script 项目的使用教程,包含了项目的目录结构、启动文件和配置文件的详细介绍。希望对您有所帮助!

登录后查看全文
热门项目推荐