Google 在 Clockify 中启动计时器的脚本
Google Script to Start Timer in Clockify
我正在尝试使用 Google 脚本在 Clockify 中启动计时器。该脚本不会给出错误(例如 400 Bad Request),但不会启动任何计时器。
API https://clockify.me/developers-api#operation--v1-workspaces--workspaceId--time-entries-post
处的文档
function StartClockifyTimer() {
// Starts a timer based on the current sheet
// Step 1: Find PID
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var FileNo = sheet.getSheetName();
var PIDheaders = {"X-Api-Key" : "[apikey]", "content-type" : "application/json"};
//var PIDpayload = JSON.stringify({'name' : FileNo});
var PIDoptions = {
'muteHttpExceptions' : true,
'method' : 'get',
'headers' : PIDheaders,
//'params' : PIDpayload
};
var PID = UrlFetchApp.getRequest('https://api.clockify.me/api/v1/workspaces/[wid]/projects/', PIDoptions);
for(i in PID) {
Logger.log(i + ": " + PID[i]);
}
//Step 2: Use PID to start timer
timezone = "GMT+" + new Date().getTimezoneOffset()/60;
var date = Utilities.formatDate(new Date(), timezone, "yyyy-MM-dd'T'HH:mm:ss'Z'");
var headers = {"X-Api-Key" : "[apikey]", "content-type" : "application/json"};
var payload = JSON.stringify({'start' : date, 'projectId' : PID});
var clockifyoptions = {
'muteHttpExceptions' : true,
'method' : 'post',
'headers' : headers,
'payload' : payload
};
var r = UrlFetchApp.fetch('https://api.clockify.me/api/v1/workspaces/[wid]/time-entries/', clockifyoptions);
Logger.log(r);
}
--编辑-- 自发布以来,感谢收到的建议,我在流程中添加了一个新步骤,以按名称查找 PID。但是,我无法使 GET 请求正常工作。我需要按照 https://clockify.me/developers-api#operation--v1-workspaces--workspaceId--projects-get 中的描述通过 FileNo 查询 'name',但我无法得到 GET 请求。即使上面的代码带有注释掉的部分 returns:
[19-08-01 11:37:46:024 MDT] getAs: function getAs() {/*
*/}
[19-08-01 11:37:46:025 MDT] getHeaders: function getHeaders() {/*
*/}
[19-08-01 11:37:46:025 MDT] getContentText: function getContentText()
{/*
*/}
[19-08-01 11:37:46:026 MDT] getContent: function getContent() {/*
*/}
[19-08-01 11:37:46:026 MDT] getResponseCode: function
getResponseCode() {/*
*/}
[19-08-01 11:37:46:027 MDT] getAllHeaders: function getAllHeaders()
{/*
*/}
[19-08-01 11:37:46:027 MDT] toString: function toString() {/*
*/}
[19-08-01 11:37:46:028 MDT] getBlob: function getBlob() {/*
*/}
我已经确认在第 2 步中使用硬编码的 ProjectID 是可行的,所以我需要帮助的实际上是第 1 步 - 找到 PID。谁能帮我格式化 GET 请求?
非常感谢。
您可以尝试一些方法来查看问题所在:
(1) 替换
UrlFetchApp.fetch('https://api.clockify.me/api/v1/workspaces/[workspaceredacted]/time-entries/', clockifyoptions);
Logger.log(clockifyoptions);
有了这个
var r = UrlFetchApp.fetch('https://api.clockify.me/api/v1/workspaces/[workspaceredacted]/time-entries/', clockifyoptions);
Logger.log(r);
这样,日志将向您显示来自 API 的响应,您可以使用它进行调试。
(2) payload中的projectId
可能是错误的。
假设 projectID
是活动 sheet 的名称,请尝试替换
var FileNo = ss.getSheetName();
和
var FileNo = sheet.getSheetName();
知道了。将代码留给其他任何试图访问 Clockify 的 API 的人。
function StartClockifyTimer() {
// Starts a timer based on the current sheet
// Step 1: Find PID
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var FileNo = sheet.getSheetName();
var url = 'https://api.clockify.me/api/v1/workspaces/[workspaceid]/projects?name='+FileNo
const header = {
"headers": {
"X-Api-Key" : "[apikey]",
"content-type" : "application/json",
"Accept" : "*/*"
}
};
var response = UrlFetchApp.fetch(url, header)
var json = response.getContentText();
var data = JSON.parse(json);
var PID = data[0]["id"];
Logger.log(PID);
//Step 2: Use PID to start timer
timezone = "GMT+" + new Date().getTimezoneOffset();
var date = Utilities.formatDate(new Date(), timezone, "yyyy-MM-dd'T'HH:mm:ss'Z'");
var headers = {"X-Api-Key" : "[apikey]", "content-type" : "application/json"};
var payload = JSON.stringify({'start' : date, 'projectId' : PID});
var clockifyoptions = {
'muteHttpExceptions' : true,
'method' : 'post',
'headers' : headers,
'payload' : payload
};
UrlFetchApp.fetch('https://api.clockify.me/api/v1/workspaces/[workspaceid]/time-entries/', clockifyoptions);
}
我正在尝试使用 Google 脚本在 Clockify 中启动计时器。该脚本不会给出错误(例如 400 Bad Request),但不会启动任何计时器。
API https://clockify.me/developers-api#operation--v1-workspaces--workspaceId--time-entries-post
处的文档function StartClockifyTimer() {
// Starts a timer based on the current sheet
// Step 1: Find PID
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var FileNo = sheet.getSheetName();
var PIDheaders = {"X-Api-Key" : "[apikey]", "content-type" : "application/json"};
//var PIDpayload = JSON.stringify({'name' : FileNo});
var PIDoptions = {
'muteHttpExceptions' : true,
'method' : 'get',
'headers' : PIDheaders,
//'params' : PIDpayload
};
var PID = UrlFetchApp.getRequest('https://api.clockify.me/api/v1/workspaces/[wid]/projects/', PIDoptions);
for(i in PID) {
Logger.log(i + ": " + PID[i]);
}
//Step 2: Use PID to start timer
timezone = "GMT+" + new Date().getTimezoneOffset()/60;
var date = Utilities.formatDate(new Date(), timezone, "yyyy-MM-dd'T'HH:mm:ss'Z'");
var headers = {"X-Api-Key" : "[apikey]", "content-type" : "application/json"};
var payload = JSON.stringify({'start' : date, 'projectId' : PID});
var clockifyoptions = {
'muteHttpExceptions' : true,
'method' : 'post',
'headers' : headers,
'payload' : payload
};
var r = UrlFetchApp.fetch('https://api.clockify.me/api/v1/workspaces/[wid]/time-entries/', clockifyoptions);
Logger.log(r);
}
--编辑-- 自发布以来,感谢收到的建议,我在流程中添加了一个新步骤,以按名称查找 PID。但是,我无法使 GET 请求正常工作。我需要按照 https://clockify.me/developers-api#operation--v1-workspaces--workspaceId--projects-get 中的描述通过 FileNo 查询 'name',但我无法得到 GET 请求。即使上面的代码带有注释掉的部分 returns:
[19-08-01 11:37:46:024 MDT] getAs: function getAs() {/* */}
[19-08-01 11:37:46:025 MDT] getHeaders: function getHeaders() {/* */}
[19-08-01 11:37:46:025 MDT] getContentText: function getContentText() {/* */}
[19-08-01 11:37:46:026 MDT] getContent: function getContent() {/* */}
[19-08-01 11:37:46:026 MDT] getResponseCode: function getResponseCode() {/* */}
[19-08-01 11:37:46:027 MDT] getAllHeaders: function getAllHeaders() {/* */}
[19-08-01 11:37:46:027 MDT] toString: function toString() {/* */}
[19-08-01 11:37:46:028 MDT] getBlob: function getBlob() {/* */}
我已经确认在第 2 步中使用硬编码的 ProjectID 是可行的,所以我需要帮助的实际上是第 1 步 - 找到 PID。谁能帮我格式化 GET 请求?
非常感谢。
您可以尝试一些方法来查看问题所在:
(1) 替换
UrlFetchApp.fetch('https://api.clockify.me/api/v1/workspaces/[workspaceredacted]/time-entries/', clockifyoptions);
Logger.log(clockifyoptions);
有了这个
var r = UrlFetchApp.fetch('https://api.clockify.me/api/v1/workspaces/[workspaceredacted]/time-entries/', clockifyoptions);
Logger.log(r);
这样,日志将向您显示来自 API 的响应,您可以使用它进行调试。
(2) payload中的projectId
可能是错误的。
假设 projectID
是活动 sheet 的名称,请尝试替换
var FileNo = ss.getSheetName();
和
var FileNo = sheet.getSheetName();
知道了。将代码留给其他任何试图访问 Clockify 的 API 的人。
function StartClockifyTimer() {
// Starts a timer based on the current sheet
// Step 1: Find PID
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var FileNo = sheet.getSheetName();
var url = 'https://api.clockify.me/api/v1/workspaces/[workspaceid]/projects?name='+FileNo
const header = {
"headers": {
"X-Api-Key" : "[apikey]",
"content-type" : "application/json",
"Accept" : "*/*"
}
};
var response = UrlFetchApp.fetch(url, header)
var json = response.getContentText();
var data = JSON.parse(json);
var PID = data[0]["id"];
Logger.log(PID);
//Step 2: Use PID to start timer
timezone = "GMT+" + new Date().getTimezoneOffset();
var date = Utilities.formatDate(new Date(), timezone, "yyyy-MM-dd'T'HH:mm:ss'Z'");
var headers = {"X-Api-Key" : "[apikey]", "content-type" : "application/json"};
var payload = JSON.stringify({'start' : date, 'projectId' : PID});
var clockifyoptions = {
'muteHttpExceptions' : true,
'method' : 'post',
'headers' : headers,
'payload' : payload
};
UrlFetchApp.fetch('https://api.clockify.me/api/v1/workspaces/[workspaceid]/time-entries/', clockifyoptions);
}