Skip to content

创建并执行脚本

通过该接口新建脚本的异步任务,利用 response 的 taskId 轮循获取脚本执行状态来获取实时执行情况

基本信息

请求方法:POST

请求路径:/api/v1/openapi/personal/files/:file_token/script

请求主机:developer.kdocs.cn

限流频次

应用类型限额
测试应用
500 次/天
正式应用
100,000 次/天

权限范围

权限值显示名称权限说明
edit_personal_files
编辑个人文档
编辑用户个人文档

Path 参数

参数必须类型说明
file_token
string

Query 参数

参数必须类型说明
access_token
string

Body 参数

参数必须类型说明
script
string
脚本
script_name
string
脚本名称,在log中显示,如果没有传则日志显示anonymous
custom_argv
object
参数对象,该参数可在脚本中使用,如示例

返回参数

参数类型说明
code
integer
错误码
+
data
data {}
响应数据

示例

请求示例(修改 A1 单元格的值)

curl --request POST \
	--url 'https://developer.kdocs.cn/api/v1/openapi/personal/files/G_xvUrobayOMXvA4IJFVq19n6epm0T7PjYphWo-N3cE/script?access_token=kvqxrspxjctdojjqdildjhonzwusaquc' \
	--data '{"script":"Application.Range(\"A1\").Value = \"bar\"","script_name":"test"}'
OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\"script\":\"Application.Range(\\\"A1\\\").Value = \\\"bar\\\"\",\"script_name\":\"test\"}");
Request request = new Request.Builder()
	.url("https://developer.kdocs.cn/api/v1/openapi/personal/files/G_xvUrobayOMXvA4IJFVq19n6epm0T7PjYphWo-N3cE/script?access_token=kvqxrspxjctdojjqdildjhonzwusaquc")
	.post(body)
	.build();

Response response = client.newCall(request).execute();
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
)

func main() {

	url := "https://developer.kdocs.cn/api/v1/openapi/personal/files/G_xvUrobayOMXvA4IJFVq19n6epm0T7PjYphWo-N3cE/script?access_token=kvqxrspxjctdojjqdildjhonzwusaquc"

	payload := strings.NewReader("{\"script\":\"Application.Range(\\\"A1\\\").Value = \\\"bar\\\"\",\"script_name\":\"test\"}")

	req, _ := http.NewRequest("POST", url, payload)

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
import http.client

conn = http.client.HTTPSConnection("developer.kdocs.cn")

payload = "{\"script\":\"Application.Range(\\\"A1\\\").Value = \\\"bar\\\"\",\"script_name\":\"test\"}"

conn.request("POST", "/api/v1/openapi/personal/files/G_xvUrobayOMXvA4IJFVq19n6epm0T7PjYphWo-N3cE/script?access_token=kvqxrspxjctdojjqdildjhonzwusaquc", payload)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))
<?php

$curl = curl_init();

curl_setopt_array($curl, [
	CURLOPT_URL => "https://developer.kdocs.cn/api/v1/openapi/personal/files/G_xvUrobayOMXvA4IJFVq19n6epm0T7PjYphWo-N3cE/script?access_token=kvqxrspxjctdojjqdildjhonzwusaquc",
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_ENCODING => "",
	CURLOPT_MAXREDIRS => 10,
	CURLOPT_TIMEOUT => 30,
	CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	CURLOPT_CUSTOMREQUEST => "POST",
	CURLOPT_POSTFIELDS => "{\"script\":\"Application.Range(\\\"A1\\\").Value = \\\"bar\\\"\",\"script_name\":\"test\"}",
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
	echo "cURL Error #:" . $err;
} else {
	echo $response;
}
const data = JSON.stringify({
	"script": "Application.Range(\"A1\").Value = \"bar\"",
	"script_name": "test"
});

const xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
	if (this.readyState === this.DONE) {
		console.log(this.responseText);
	}
});

xhr.open("POST", "https://developer.kdocs.cn/api/v1/openapi/personal/files/G_xvUrobayOMXvA4IJFVq19n6epm0T7PjYphWo-N3cE/script?access_token=kvqxrspxjctdojjqdildjhonzwusaquc");

xhr.send(data);
const http = require("https");

const options = {
	"method": "POST",
	"hostname": "developer.kdocs.cn",
	"port": null,
	"path": "/api/v1/openapi/personal/files/G_xvUrobayOMXvA4IJFVq19n6epm0T7PjYphWo-N3cE/script?access_token=kvqxrspxjctdojjqdildjhonzwusaquc",
	"headers": {}
};

const req = http.request(options, function (res) {
	const chunks = [];

	res.on("data", function (chunk) {
		chunks.push(chunk);
	});

	res.on("end", function () {
		const body = Buffer.concat(chunks);
		console.log(body.toString());
	});
});

req.write(JSON.stringify({script: 'Application.Range("A1").Value = "bar"', script_name: 'test'}));
req.end();
CURL *hnd = curl_easy_init();

curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, "https://developer.kdocs.cn/api/v1/openapi/personal/files/G_xvUrobayOMXvA4IJFVq19n6epm0T7PjYphWo-N3cE/script?access_token=kvqxrspxjctdojjqdildjhonzwusaquc");

curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{\"script\":\"Application.Range(\\\"A1\\\").Value = \\\"bar\\\"\",\"script_name\":\"test\"}");

CURLcode ret = curl_easy_perform(hnd);
var client = new RestClient("https://developer.kdocs.cn/api/v1/openapi/personal/files/G_xvUrobayOMXvA4IJFVq19n6epm0T7PjYphWo-N3cE/script?access_token=kvqxrspxjctdojjqdildjhonzwusaquc");
var request = new RestRequest(Method.POST);
request.AddParameter("undefined", "{\"script\":\"Application.Range(\\\"A1\\\").Value = \\\"bar\\\"\",\"script_name\":\"test\"}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

返回示例(修改 A1 单元格的值)

{
  "code": 0,
  "data": {
    "task_id": "x8glVOrzTcrTZ4pNdKdpckzBbbhIUqgfhwO/SIZYUf98iGGAbW9qQYu5G35/mgH/MWdrugo0YgcWPzKRIrm8ji1oYH3/ps1qQf8XcIATebDKF5kttPEUZyu2f+b6hI5gwcI3cXMv0xTN9A==",
    "task_type": "script"
  }
}

利用该 taskid 获取的执行情况(已忽略脚本开始结束的日志内容)

{
  "error": "",
  "status": "finish",
  "data": {
    "result": "bar"
  }
}

请求示例(自定义参数)

脚本可以作为某项功能的执行模板,模板中的可变参数可以在 custom_argv 中动态更改

curl --request POST \
	--url 'https://developer.kdocs.cn/api/v1/openapi/personal/files/G_xvUrobayOMXvA4IJFVq19n6epm0T7PjYphWo-N3cE/script?access_token=kvqxrspxjctdojjqdildjhonzwusaquc' \
	--data '{"script":"console.log(argv.bar)","script_name":"test","custom_argv":{"bar":"foo"}}'
OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\"script\":\"console.log(argv.bar)\",\"script_name\":\"test\",\"custom_argv\":{\"bar\":\"foo\"}}");
Request request = new Request.Builder()
	.url("https://developer.kdocs.cn/api/v1/openapi/personal/files/G_xvUrobayOMXvA4IJFVq19n6epm0T7PjYphWo-N3cE/script?access_token=kvqxrspxjctdojjqdildjhonzwusaquc")
	.post(body)
	.build();

Response response = client.newCall(request).execute();
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io/ioutil"
)

func main() {

	url := "https://developer.kdocs.cn/api/v1/openapi/personal/files/G_xvUrobayOMXvA4IJFVq19n6epm0T7PjYphWo-N3cE/script?access_token=kvqxrspxjctdojjqdildjhonzwusaquc"

	payload := strings.NewReader("{\"script\":\"console.log(argv.bar)\",\"script_name\":\"test\",\"custom_argv\":{\"bar\":\"foo\"}}")

	req, _ := http.NewRequest("POST", url, payload)

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := ioutil.ReadAll(res.Body)

	fmt.Println(res)
	fmt.Println(string(body))

}
import http.client

conn = http.client.HTTPSConnection("developer.kdocs.cn")

payload = "{\"script\":\"console.log(argv.bar)\",\"script_name\":\"test\",\"custom_argv\":{\"bar\":\"foo\"}}"

conn.request("POST", "/api/v1/openapi/personal/files/G_xvUrobayOMXvA4IJFVq19n6epm0T7PjYphWo-N3cE/script?access_token=kvqxrspxjctdojjqdildjhonzwusaquc", payload)

res = conn.getresponse()
data = res.read()

print(data.decode("utf-8"))
<?php

$curl = curl_init();

curl_setopt_array($curl, [
	CURLOPT_URL => "https://developer.kdocs.cn/api/v1/openapi/personal/files/G_xvUrobayOMXvA4IJFVq19n6epm0T7PjYphWo-N3cE/script?access_token=kvqxrspxjctdojjqdildjhonzwusaquc",
	CURLOPT_RETURNTRANSFER => true,
	CURLOPT_ENCODING => "",
	CURLOPT_MAXREDIRS => 10,
	CURLOPT_TIMEOUT => 30,
	CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
	CURLOPT_CUSTOMREQUEST => "POST",
	CURLOPT_POSTFIELDS => "{\"script\":\"console.log(argv.bar)\",\"script_name\":\"test\",\"custom_argv\":{\"bar\":\"foo\"}}",
]);

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
	echo "cURL Error #:" . $err;
} else {
	echo $response;
}
const data = JSON.stringify({
	"script": "console.log(argv.bar)",
	"script_name": "test",
	"custom_argv": {
		"bar": "foo"
	}
});

const xhr = new XMLHttpRequest();
xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () {
	if (this.readyState === this.DONE) {
		console.log(this.responseText);
	}
});

xhr.open("POST", "https://developer.kdocs.cn/api/v1/openapi/personal/files/G_xvUrobayOMXvA4IJFVq19n6epm0T7PjYphWo-N3cE/script?access_token=kvqxrspxjctdojjqdildjhonzwusaquc");

xhr.send(data);
const http = require("https");

const options = {
	"method": "POST",
	"hostname": "developer.kdocs.cn",
	"port": null,
	"path": "/api/v1/openapi/personal/files/G_xvUrobayOMXvA4IJFVq19n6epm0T7PjYphWo-N3cE/script?access_token=kvqxrspxjctdojjqdildjhonzwusaquc",
	"headers": {}
};

const req = http.request(options, function (res) {
	const chunks = [];

	res.on("data", function (chunk) {
		chunks.push(chunk);
	});

	res.on("end", function () {
		const body = Buffer.concat(chunks);
		console.log(body.toString());
	});
});

req.write(JSON.stringify({
  script: 'console.log(argv.bar)',
  script_name: 'test',
  custom_argv: {bar: 'foo'}
}));
req.end();
CURL *hnd = curl_easy_init();

curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_URL, "https://developer.kdocs.cn/api/v1/openapi/personal/files/G_xvUrobayOMXvA4IJFVq19n6epm0T7PjYphWo-N3cE/script?access_token=kvqxrspxjctdojjqdildjhonzwusaquc");

curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "{\"script\":\"console.log(argv.bar)\",\"script_name\":\"test\",\"custom_argv\":{\"bar\":\"foo\"}}");

CURLcode ret = curl_easy_perform(hnd);
var client = new RestClient("https://developer.kdocs.cn/api/v1/openapi/personal/files/G_xvUrobayOMXvA4IJFVq19n6epm0T7PjYphWo-N3cE/script?access_token=kvqxrspxjctdojjqdildjhonzwusaquc");
var request = new RestRequest(Method.POST);
request.AddParameter("undefined", "{\"script\":\"console.log(argv.bar)\",\"script_name\":\"test\",\"custom_argv\":{\"bar\":\"foo\"}}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);

返回示例(自定义参数)

利用该 task 获取执行情况(已忽略脚本开始结束的日志内容)

{
  "error": "",
  "status": "finish",
  "data": {
    "logs": [
      {
        "args": ["foo"],
        "filename": "test.ks:1:1",
        "unix_time": 1667462203,
        "level": "info",
        "timestamp": "17:38:53.839"
      }
    ],
    "result": "[Undefined]"
  }
}

错误码

请参考错误码说明