← 提示词库 Anthropic/claude-code/skills/google-workspace/references/sheets.md 原文 md
🌐 中英双语对照

Google Sheets reference / Google Sheets 参考文档

As a first step, before you create a sheet or change one, you must read the spreadsheet design rules below in full. After them, this file covers the Sheets connector (get_spreadsheet, get_values, update_values, update_formulas, insert_dimension, and update_spreadsheet) and how to carry out the design rules with it. Most of the connector behavior here was tested against Google's Sheets API; the rest was seen through the connector, and a few rules have not been checked yet.

作为第一步,在创建或修改电子表格之前,你必须完整阅读下文的电子表格设计规则。在这些规则之后,本文件介绍 Sheets 连接器(get_spreadsheet、get_values、update_values、update_formulas、insert_dimension 与 update_spreadsheet),以及如何用它落实设计规则。此处关于连接器行为的大部分内容都对照 Google 的 Sheets API 做过测试;其余内容是通过连接器观察到的,还有少数规则尚未核实。

Spreadsheet design / 电子表格设计

When the user asks for something specific, such as tab names, column headers, a formula or a number format, do exactly that, and don't replace it with a design of your own; these rules decide what the user left open. When you edit an existing workbook, its conventions come first (see "Editing an existing workbook"). The rules for wording sheet names, headers, labels and notes are under "Writing in the workbook".

当用户提出明确要求时,例如标签页名称、列标题、某个公式或数字格式,就严格照做,不要换成你自己的设计;这些规则只决定用户未加规定的部分。编辑既有工作簿时,其既有约定优先(见 "Editing an existing workbook")。关于工作表名称、表头、标签与备注措辞的规则见 "Writing in the workbook"。

A reader judges a spreadsheet by whether its numbers are right and whether they can check them. Build it so that clicking any number shows either a formula they can trace or a labeled input they can change.

读者评判一份电子表格,看的是数字是否正确、以及他们能否自行核验。构建时应做到:点击任何一个数字,看到的要么是一条可追溯的公式,要么是一个带标签、可供修改的输入。

Formulas, not typed results / 用公式,而不是键入的结果

Before you finish, check that a reader can click any number in the analysis and see how it was derived. Replace any bare value that should be a formula.

收尾之前,检查读者能否点击分析中的任何一个数字并看到它的推导方式。把任何本应是公式的裸数值替换掉。

Inputs and hardcoded values / 输入与硬编码值

Before you write a value, ask: Is it a business assumption? Put it in a labeled cell. Is it derived? Write the formula. Is it an input with no source in the workbook? Label it and say where it came from.

写入任何一个值之前先问:它是业务假设吗?放进带标签的单元格。它是派生的吗?写公式。它是工作簿中没有来源的输入吗?加上标签并说明来源。

Formulas a reader can follow / 读者能够看懂的公式

Data that grows / 会增长的数据

Layout / 版面

Writing in the workbook / 在工作簿中写作

Text you write in the workbook (sheet names, headers, labels, notes, comments, text cells, chart titles) should read as though a person wrote it. When readers think something was written by AI, they judge it as sloppy and stop trusting it, whatever the content. They make that judgment from a set of common indicators, listed below, so take extra care to keep them out of your writing. These rules are for text you write, and a style the user or their style guide asks for takes priority. Do not rewrite the user's existing text to follow them unless the user asks you to.

写入工作簿的文本(工作表名称、表头、标签、备注、批注、文本单元格、图表标题)读起来应当像出自真人之手。当读者认为某段文字是 AI 写的,无论内容如何,他们都会视之为粗制滥造并不再信任它。他们依据一组常见特征做出这种判断,特征列举如下,因此要格外注意不让它们出现在你的文字里。这些规则针对你写的文本;用户或其风格指南要求的样式优先。除非用户要求,否则不要为了遵守这些规则而改写用户的既有文本。

【评论】这一节以"防止输出被识别为机器写作"为目标,列举了破折号滥用、"恰好三项"列表、空泛形容词等英文语料中常见的 LLM 文风特征,属于面向文风真实性的约束。

Financial models / 财务模型

Use these unless the user or the existing file does something else.

除非用户或既有文件另有做法,否则使用以下约定。

Text colors:
文本颜色:

Number formats:
数字格式:

Sensitivity tables:
敏感性分析表:

Charts / 图表

Sources and citations / 来源与引用

Every value that comes into the workbook from outside it should be traceable without asking you: where it came from, how it reached you, and when it was pulled.

从工作簿外部进入工作簿的每一个值,都应能在不询问你的情况下被追溯:它来自哪里、如何到达你、何时抓取。

【评论】该节要求把个人身份信息在审计记录中替换为 [REDACTED],同时保留查询的可读性与可重跑性,是在数据合规与可复现性之间做的折中。

Editing an existing workbook / 编辑既有工作簿

Checking the result / 检查结果

Sheets connector / Sheets 连接器

Applying the design rules / 落实设计规则

Create / 创建

How a sheet is addressed / 工作表的寻址方式

There are two coordinate systems, and each tool uses one of them.

这里有两套坐标系统,每个工具使用其中一套。

Tools Addresses cells by Example
get_values, update_values, update_formulas A1 notation with the tab name 'P&L Model'!B4:D10
update_spreadsheet, insert_dimension Numeric sheetId plus 0-based indexes; end indexes are exclusive B4:D10 on tab 1001 is {"sheetId": 1001, "startRowIndex": 3, "endRowIndex": 10, "startColumnIndex": 1, "endColumnIndex": 4}
工具 单元格寻址方式 示例
get_values、update_values、update_formulas A1 记法加标签页名称 'P&L Model'!B4:D10
update_spreadsheet、insert_dimension 数字 sheetId 加 0 起始索引;结束索引不含端点 标签页 1001 上的 B4:D10 即 {"sheetId": 1001, "startRowIndex": 3, "endRowIndex": 10, "startColumnIndex": 1, "endColumnIndex": 4}
python <skill>/scripts/sheets_helper.py range "'P&L Model'!B4:D10" --meta meta.json
{"sheetId": 1001, "startRowIndex": 3, "endRowIndex": 10, "startColumnIndex": 1, "endColumnIndex": 4}

meta.json is the (small) output of the metadata read below, written to a file.

meta.json 是下文元数据读取的(体积很小的)输出,写入文件保存。

Read / 读取

{"spreadsheetId": "...", "includeGridData": true, "ranges": ["'Model'!A1:H40"],
 "fields": ["sheets.properties.title", "sheets.data.startRow", "sheets.data.startColumn",
            "sheets.data.rowData.values.userEnteredValue",
            "sheets.data.rowData.values.formattedValue",
            "sheets.data.rowData.values.effectiveValue"]}

Then list the cells, with each formula next to its displayed result:

然后列出单元格,每条公式旁标注其显示结果:

python <skill>/scripts/sheets_helper.py cells grid.json
Sheet1!D5   '=B5*(1+C5)'   -> '$125'
Sheet1!D7   '=D5/0'        -> '#DIV/0!'  <-- ERROR DIVIDE_BY_ZERO
errors: 1

Add --errors-only to list only the error cells. Add sheets.data.rowData.values.userEnteredFormat to the fields when you need to read formatting, and keep the range tight: grid reads grow fast.

加 --errors-only 只列出错误单元格。需要读取格式时,把 sheets.data.rowData.values.userEnteredFormat 加进 fields,并保持区间紧凑:网格读取的体积增长很快。

Write values and formulas / 写入值与公式

Batch updates: structure, formatting, charts / 批量更新:结构、格式与图表

update_spreadsheet sends raw spreadsheets.batchUpdate requests. The reads in this file return no revisionId, so send these requests without writeControl, and read again right before a write that depends on positions.

update_spreadsheet 发送原始的 spreadsheets.batchUpdate 请求。本文件中的读取不返回 revisionId,因此发送这些请求时不带 writeControl,并在执行依赖位置的写入之前先重新读取。

{"sheet": "Model",
 "rules": [
   {"range": "A1:F1", "bold": true, "bg": "#1F3864", "fg": "#FFFFFF", "align": "center"},
   {"range": "B2:B8", "fg": "#0000FF"},
   {"range": "C2:F20", "number": "currency"},
   {"range": "G2:G20", "number": "percent"},
   {"range": "A1:G20", "borders": "#BFBFBF"}],
 "freeze": {"rows": 1, "columns": 1},
 "col_widths": {"A": 220, "B:G": 110},
 "row_heights": {"1": 30}}
python <skill>/scripts/sheets_helper.py format spec.json --meta meta.json

For one tab whose sheetId you already know, such as 0 on a new sheet, pass --sheet-id 0 instead of --meta and leave sheet out of the spec. The output is the full requests for update_spreadsheet. Named number formats: currency, currency2, percent, multiple, integer, decimal, date, text; any other string is used as a Sheets pattern. Rule keys: bold, italic, size, font, fg, bg, align, valign, wrap, number, borders.

对已知道 sheetId 的单个标签页(例如新表的 0),传 --sheet-id 0 代替 --meta,并在 spec 中省去 sheet。输出即 update_spreadsheet 所需的完整 requests。命名数字格式:currency、currency2、percent、multiple、integer、decimal、date、text;其他任何字符串都会作为 Sheets 模式使用。规则键:bold、italic、size、font、fg、bg、align、valign、wrap、number、borders。

{"addChart": {"chart": {
  "spec": {"title": "Revenue", "basicChart": {"chartType": "COLUMN",
    "domains": [{"domain": {"sourceRange": {"sources": [
      {"sheetId": 0, "startRowIndex": 0, "endRowIndex": 6, "startColumnIndex": 0, "endColumnIndex": 1}]}}}],
    "series": [{"series": {"sourceRange": {"sources": [
      {"sheetId": 0, "startRowIndex": 0, "endRowIndex": 6, "startColumnIndex": 1, "endColumnIndex": 2}]}},
      "targetAxis": "LEFT_AXIS"}],
    "headerCount": 1}},
  "position": {"overlayPosition": {"anchorCell": {"sheetId": 0, "rowIndex": 8, "columnIndex": 0}}}}}}

Use LINE for trends over time, BAR or COLUMN for comparisons, and set headerCount: 1 when the first row is a label.

趋势随时间变化用 LINE,比较用 BAR 或 COLUMN,首行是标签时设 headerCount: 1。

Verify / 验证

  1. Read the displayed values of everything you built with get_values. Scan for #REF!, #DIV/0!, #NAME?, #VALUE!, #N/A, and #ERROR!, and check that totals match the numbers you expect.
    1. 用 get_values 读取你构建的一切的显示值。扫描 #REF!、#DIV/0!、#NAME?、#VALUE!、#N/A 与 #ERROR!,并核对合计与你预期的数字一致。
  2. For models, run the grid read and sheets_helper.py cells --errors-only, then spot-check that calculated cells hold formulas ('=...'), not typed numbers.
    1. 对模型,运行网格读取与 sheets_helper.py cells --errors-only,再抽查计算单元格持有的是公式('=...')而不是键入的数字。
  3. After an upload, confirm the tab names and sizes with the metadata read before editing.
    1. 上传之后,编辑前先用元数据读取确认标签页名称与大小。

Function support / 函数支持

Sheets supports XLOOKUP, FILTER, UNIQUE, and SORT, so the xlsx skill's LibreOffice limits do not apply here. If the user may export the file to Excel, avoid Sheets-only functions such as QUERY, ARRAYFORMULA, IMPORTRANGE, and GOOGLEFINANCE. Formulas that fetch an outside URL (IMAGE, IMPORTDATA, IMPORTXML, IMPORTHTML, IMPORTFEED) show #REF! until someone opens the sheet in a browser and clicks Allow access, so tell the user.

Sheets 支持 XLOOKUP、FILTER、UNIQUE 与 SORT,因此 xlsx 技能中的 LibreOffice 限制在此不适用。如果用户可能把文件导出到 Excel,避免使用 Sheets 专属函数如 QUERY、ARRAYFORMULA、IMPORTRANGE 与 GOOGLEFINANCE。抓取外部 URL 的公式(IMAGE、IMPORTDATA、IMPORTXML、IMPORTHTML、IMPORTFEED)在有人于浏览器中打开该表并点击 Allow access 之前会显示 #REF!,所以要告知用户。

Sheets failures / Sheets 故障排查

Symptom Cause Fix
Invalid field: sheets.properties(sheetId,title) The connector rejects parenthesized field masks Pass one full path per array item: ["sheets.properties"].
Invalid field: user_entered_format(number_format Parentheses in a request's fields Use comma-separated full paths. sheets_helper.py format does this for you.
An ID lost its leading zeros, or text became a date Both write tools parse input like the UI Prefix the value with '.
A formula shows up as a value you typed A computed result was written instead of a formula Write the formula string starting with =.
get_values result has no values key The range is empty Treat it as empty; it is not an error.
Formatting landed on the wrong rows 1-based rows used as 0-based indexes, or an inclusive end index Build the range with sheets_helper.py range.
Formatting landed on the wrong tab Tab position or name used as sheetId Read sheets.properties and use the numeric sheetId.
Formulas point at the wrong rows after an insert Rows were written using positions from before the insert Re-read after insertDimension; Sheets already shifted existing formulas.
Unable to parse range: <tab>!A1 Usually a tab name that doesn't exist, not bad A1 syntax Read sheets.properties and use the exact tab title.
症状 原因 修复
Invalid field: sheets.properties(sheetId,title) 连接器拒绝带括号的字段掩码 每个数组项传一条完整路径:["sheets.properties"]。
Invalid field: user_entered_format(number_format 请求的 fields 中出现括号 使用逗号分隔的完整路径。sheets_helper.py format 会替你这样做。
ID 丢失了前导零,或文本变成了日期 两个写入工具都按 UI 的方式解析输入 给值加前缀 '。
公式显示成了你键入的值 写入的是计算结果而不是公式 写以 = 开头的公式字符串。
get_values 结果没有 values 键 区间为空 当作空处理;这不是错误。
格式落在了错误的行上 把 1 起始的行号当成了 0 起始索引,或把结束索引当成了含端点 用 sheets_helper.py range 构建区间。
格式落在了错误的标签页上 把标签页位置或名称当成了 sheetId 读取 sheets.properties 并使用数字 sheetId。
插入行之后公式指向错误的行 用插入前的位置写入了行 insertDimension 之后重新读取;Sheets 已平移既有公式。
Unable to parse range: <tab>!A1 通常是标签页名称不存在,而不是 A1 语法错误 读取 sheets.properties 并使用确切的标签页标题。