我正在嘗試撰寫一個 POST 方法 Google Apps Script (對于一個四處嘗試學習的人來說,這是一個相當大的成就)并且已經管理了我的目標的第一步,即一個有效的初始腳本 - 因為它運行和在 Zendesk 中創建一個欄位,提供我“硬代碼”/在腳本中顯式寫入鍵值對(如下所示)。
我現在一直在嘗試做的是遍歷 Sheet 中的行以獲取每個行/條目的鍵值對和 POST(?),從而允許我只需在作業表中輸入資料即可創建多個欄位。
我確信這將是一個 for 回圈,但我已經碰到了一堵磚墻,試圖真正弄清楚它,并希望這里有人可以幫忙。
如果有意義,我將我的資料放在 A:B 列(屬性是型別和標題)的作業表中?我很沮喪,因為我知道足以讓我繼續前進,但不知道如何完成它:-(
function CreateField2(){
var sheet = SpreadsheetApp.getActiveSheet(); // data i want to use is here in columns A:B (type, text) - this will be expanded to other attributes eventually
//this is my data explicitly called out which works fine
var data = {"ticket_field": {
"type": "text", "title": "Age"}
};
//how do i take the values from my sheet and use them here?
var url = 'https://url.com/api/v2/ticket_fields';
var user = '[email protected]/token';
var pwd = 'myAPItokenHere';
var options = {
'method' : 'post',
'headers': {
'Authorization': "Basic " Utilities.base64Encode(user ':' pwd)
},
'payload' : JSON.stringify(data),
'contentType': 'application/json',
'muteHttpExceptions': true
};
UrlFetchApp.fetch(url, options);
}
我在作業表中的資料將如下所示:

更新:以下關于物件與物件陣列的評論。我認為我真正需要做的是改變回圈,以便它為每次迭代運行 post 方法?添加了以下內容:
// Can i change the array of objects into separate objects?
const things = [
data // this is the data gathered using either of the proposed methods
];
const filteredArr = things.reduce((thing, current) => {
const x = thing.find(item => item.place === current.place);
if (!x) {
return thing.concat([current]);
} else {
return thing;
}
}, []);
console.log(filteredArr)
// End of newly added code
//Note: Not sure this is the right approach, i think i might need to to iterate over and perform the post function for each one?
uj5u.com熱心網友回復:
替換這個: -
var sheet = SpreadsheetApp.getActiveSheet(); // data i want to use is here in columns A:B (type, text) - this will be expanded to other attributes eventually
//this is my data explicitly called out which works fine
var data = {"ticket_field": {
"type": "text", "title": "Age"}
};
有了這個:-
const ss = SpreadsheetApp.getActiveSpreadsheet()
const ssSource = ss.getSheetByName('Sheet1')
const dataRange = ssSource.getDataRange().getValues();
var result = [];
var head = dataRange[0]; // Getting Head Row
var cols = head.length;
var row = [];
for (var i = 1; i < dataRange.length; i )
{
row = dataRange[i]; // Getting data Rows
var obj = {}; // Clearing Object
for (var col = 0; col < cols; col )
{
obj[head[col]] = row[col]; // Assigning values to Keys
}
result.push(obj); // Pushing Object
}
const data = JSON.stringify({ "ticket_field" : result}).replace(/[[\]]/g, '')
JSON.stringify您可以在選項引數中洗掉
參考:
大批
代替
uj5u.com熱心網友回復:
嘗試
function table2json() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('YourSheetName');
var [headers, ...rows] = sheet.getDataRange().getValues();
var data = {}
var items = []
rows.forEach(function(r) {
var obj={}
r.forEach(function (c, j) {
obj[headers[j]] = c
})
items.push(obj)
})
data['ticket_field'] = items
Logger.log(JSON.stringify(data))
}
你會得到
{"ticket_field":[{"type":"text","title":"Summary"},{"type":"Multi-line text","title":"Description"},{"type":"Drop-down list","title":"Choose a thing"}, ... ]}
uj5u.com熱心網友回復:
謝謝你們倆; 經過幾個小時或深思熟慮(谷歌搜索/反復試驗),我讓它使用以下代碼:
function CreateFields(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
var [headers, ...rows] = sheet.getDataRange().getValues();
var data = {}
var items = []
rows.forEach(function(r) {
var obj={}
r.forEach(function (c, j) {
obj[headers[j]] = c
})
var data = {}//moved
data['ticket_field'] = obj // moved this inside your loop
items.push(data) // pushed the object into the items array
})
Logger.log(JSON.stringify(items))
items.forEach(function(i) { // added this to loop over objects in items
var url = 'https://urlhere.com/api/v2/ticket_fields';
var user = 'myemailaddress/token';
var pwd = 'mytokenhere';
var options = {
'method' : 'post',
'headers': {
'Authorization': "Basic " Utilities.base64Encode(user ':' pwd)
},
'payload' : JSON.stringify(i),
'contentType': 'application/json',
'muteHttpExceptions': true
};
UrlFetchApp.fetch(url, options);
Logger.log(i)
})
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/437368.html
標籤:json 谷歌应用脚本 邮政 zendesk-api
