# Google Docs

![使用示範](/files/GCq2CU4BdmLxkoh3X1ST)

## 準備方法

打開你需要擲骰的文件，在Tools -> Scripts Editor 中，\
把最下面代碼取代進去。選擇init

## 使用方法

有兩種方式

1. 直接按Run，直接啓動程式。
2. 按畫面左邊時鐘符號Triggers，再按右下角Add Trigger，選擇init，\
   然後按Save。以後每次重啓Docs 畫面時，就會自動擲骰。

## 代碼

```
async function init() {
  do {
    var next = await replaceHKTRPG_Text();
  } while (next);

}

async function replaceHKTRPG_Text() {
  let body = DocumentApp.getActiveDocument().getBody();
  let target = body.findText("^(?im)/hk\\s+(.*)");
  if (!target) return;
  let text = target.getElement().asText().getText();
  let result = await HKTRPG_Api(text.replace(/\/hk\s+/i, ''))
  target.getElement().asText().setText('HKTRPG\n' + result.message);
  return true;
}


async function HKTRPG_Api(query = 'cc 80') {
  if (!query) return;
  var url = 'https://api.hktrpg.com/'
    + '?msg=' + encodeURIComponent(query);
  var response = UrlFetchApp.fetch(url, { 'muteHttpExceptions': true });
  var json = response.getContentText();
  var data = JSON.parse(json);
  return data;
}


```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bothelp.hktrpg.com/hktrpg-guan-fang-shi-yong-jiao-xue/kai-shi-shi-yong/yao-qing-hktrpg/google-docs.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
