【发布时间】:2020-02-12 15:46:37
【问题描述】:
我的代码应该从 google sheet 获取一些数据并通过外部 API POST 到系统。但是,当我运行代码时,我一直遇到错误 404。
调试时,表示response没有定义。这是有问题的代码的药水。我错过了什么吗?
function postLeave(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
// used getLastRow() function [1] to narrow the array to have only cells with data.
var range = sheet.getRange("K2:K"+sheet.getLastRow()).getValues();
var searchString = "";
for (var i = 0; i<range.length; i++) {
if(range[i][0] == searchString) {
var lastRow = sheet.getRange(2+i,1,1,10).getValues();
var userid = sheet.getRange("I2:I" + sheet.getLastRow()).getValues();
var data = {
//'user_id': lastRow[0][8],
"leave_type_id":lastRow[0][9],
"date":lastRow[0][7],
"hours":lastRow[0][6],
};
var payload = JSON.stringify(data);
var options = {
'method': 'POST',
'Content-Type': 'application/json',
'payload' : data,
};
var url = 'https://api.10000ft.com/api/v1/users/' + userid + '/time_entries?auth=key;
var response = UrlFetchApp.fetch(url).getContentText();
if (response === 200) {
//var json = JSON.parse(response);
sheet.getRange(2+i, 11).setValue(1);
}
else {
sheet.getRange(2+i, 11).setValue(0);
Logger.log(response)
}
}
}
}
文档
挑战端点。
https://github.com/10Kft/10kft-api/blob/master/sections/assignables.md
https://github.com/10Kft/10kft-api/blob/master/sections/leave-types.md https://github.com/10Kft/10kft-api/blob/master/sections/time-entries.md
【问题讨论】:
-
可能
'payload' : data,应该是'payload' : payload,,因为你想传递字符串化的值? -
能否提供你要使用的API方法的官方文档?
-
@Tanaike 请在此链接中找到它github.com/10Kft/10kft-api/blob/master/sections/time-entries.md
-
@AaronDuniganAtLee,我已经尝试过了,但仍然遇到同样的错误
-
感谢您的回复。当我看到链接的创建时间条目的文档时,没有使用
project_id。但是脚本中的data具有project_id的属性。而且,您的脚本和文档之间的端点似乎不同。在你的脚本中,虽然使用了api/v1/users,但没有使用user_id,而是使用了project_id。所以我不明白你想在 API 中使用什么方法。我可以问你这些吗?
标签: post google-apps-script google-sheets response urlfetch