簡體   English   中英

從 Salesbinder API JSON 獲取特定數據到 Google 表格

[英]Get specific data from Salesbinder API JSON to a Google sheet

您好,我想做的是從 Salesbinder 發票 API 獲取數據(發票編號取自 Sheet2 Cell A1),將數據輸出到 Google 工作表(Sheet Cell A2)
這是我用來從 Salesbinder API 獲取數據的代碼

function fetching() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet1');
  var sheet2 = ss.getSheetByName('Sheet2');
  var InvNumber = sheet2.getRange('A1').getValue();
  var USERNAME = 'APIKey';
  var PASSWORD = 'x';
  
  var url = 'https://mydomain.salesbinder.com/api/2.0/documents.json?documentNumber='+InvNumber+'&contextId=5';
  var headers = {
    "Authorization": "Basic " + Utilities.base64Encode(USERNAME + ':' + PASSWORD)
  };

 
  let response = UrlFetchApp.fetch(url, { headers });
  Logger.log(response.getContentText());

}

我從數據中得到的結果是這樣的

{“文檔”:[{“文檔編號”:8542,“名稱”:“紫羅蘭軸承”,“context_id”:5,“total_cost”:7213.03,“total_tax”:415.45,“total_tax2”:0,“total_price”: 8309.08,"total_transactions":0,"issue_date":"2022-12-13T00:00:00+00:00","expiry_date":null,"shipping_address":"(同上)","date_sent":空,“shipped_percent”:空,“status_id”:9,“public_note”:“”,“注意”:“GLEN”,“payment_terms”:“”,“id”:“0b24e8d2-e7cc-4441-abd7-337c8a2c6cf1 ","customer_id":"5b0d881b-6a88-46e5-998c-06330a8e0006","user_id":"5b0d75b3-e9cc-4899-bf99-77610a8e0006","associated_document_id":"","created":"2022-12- 13T21:21:43+00:00","modified":"2022-12-21T16:01:04+00:00","status":{"id":9,"name":"unpaid"} ,"context":{"id":5,"name":"invoice"},"customer":{"id":"5b0d881b-6a88-46e5-998c-06330a8e0006","name":"TestClient", "customer_number":1889,"billing_address_1":"Add1","billing_address_2":"Add2","billing_city":"TestCity","billing_region":"TestLoc","billing_postal_code":"TestPost","billing_country": “加拿大”,“shipping_address_1”:“Add1”,“s hipping_address_2":"Add2","shipping_city":"TestCity","shipping_region":"TestLoc","shipping_postal_code":"TestPost","shipping_country":"加拿大"},"user":{"first_name":" test","last_name":"test"},"document_items":[{"id":"0ea11906-f197-4eb4-971e-715d4dc77ab2","name":"B-0832","document_id":"0b24e8d2 -e7cc-4441-abd7-337c8a2c6cf1","item_id":null,"unit_id":4,"service_category_id":"5dc2f6d2-27e0-4dac-a2b7-39080a8e0008","description":"JGE/H/K DOOR GASKET ","quantity":8,"quantity_partially_received":0,"quantity_partially_shipped":0,"tax":5,"tax2":0,"discount_percent":0,"cost":38.1,"price":43.81, “discounted_price”:0,“weight”:2,“modified”:“2022-12-21T16:01:04 + 00:00”,“created”:“2022-12-15T15:43:57 + 00:00 ","item_variations_location_id":null,"item_variation_data":null,"delete":false,"item":null},{"id":"2b7e9376-ed70-4e75-95c9-6237aad8cfc0","name":"B -0770","document_id":"0b24e8d2-e7cc-4441-abd7-337c8a2c6cf1","item_id":null,"unit_id":4,"service_category_id":"5dc2f6d2-27e0-4dac-a2b7-39080a8e0008","描述 n":"MAIN BEARING JGE/H/K","quantity":8,"quantity_partially_received":0,"quantity_partially_shipped":0,"tax":5,"tax2":0,"discount_percent":0," cost":282.73,"price":325.14,"discounted_price":0,"weight":6,"modified":"2022-12-21T16:01:04+00:00","created":"2022- 12-15T15:43:57+00:00","item_variations_location_id":null,"item_variation_data":null,"delete":false,"item":null},{"id":"391c2ec1-e02d-4390- 9a10-429c2ea460fc","name":"B-2082","document_id":"0b24e8d2-e7cc-4441-abd7-337c8a2c6cf1","item_id":null,"unit_id":4,"service_category_id":"5dc2f6d2- 27e0-4dac-a2b7-39080a8e0008","描述":"桿軸承 JGE/H/K","數量":8,"quantity_partially_received":0,"quantity_partially_shipped":0,"tax":5,"tax2" :0,"discount_percent":0,"cost":282.73,"price":325.14,"discounted_price":0,"weight":5,"modified":"2022-12-21T16:01:04+00: 00","created":"2022-12-15T15:43:57+00:00","item_variations_location_id":null,"item_variation_data":null,"delete":false,"item":null},{" ID":"725fb97d-243f-44aa-98d0-50d903871ae4","名稱": “B-0776”,“document_id”:“0b24e8d2-e7cc-4441-abd7-337c8a2c6cf1”,“item_id”:null,“unit_id”:4,“service_category_id”:“5dc2f6d2-27e0-4dac-a2b7-39080a8e0008”, “描述”:“推力板 JGE/H/K”,“數量”:2,“已接收部分數量”:0,“已部分發貨”:0,“稅收”:5,“稅收 2”:0,“discount_percent”:0, “成本”:878.44,“價格”:1010.21,“折扣價格”:0,“重量”:7,“修改”:“2022-12-21T16:01:04 + 00:00”,“創建”:“2022 -12-15T15:43:57+00:00","item_variations_location_id":null,"item_variation_data":null,"delete":false,"item":null},{"id":"956cf37e-3921-4cad -94bf-670cdedd2d17","name":"B-1032","document_id":"0b24e8d2-e7cc-4441-abd7-337c8a2c6cf1","item_id":null,"unit_id":4,"service_category_id":"5dc2f6d2 -27e0-4dac-a2b7-39080a8e0008","描述":"JGK DOOR GASKET","數量":8,"quantity_partially_received":0,"quantity_partially_shipped":0,"tax":5,"tax2":0, "discount_percent":0,"cost":17.21,"price":19.79,"discounted_price":0,"weight":3,"modified":"2022-12-21T16:01:04+00:00", “創建”:“2022-12-15T15 :43:57+00:00","item_variations_location_id":null,"item_variation_data":null,"delete":false,"item":null},{"id":"c2b74c55-465b-4bee-84a1-6c9e8e5bd102 ","name":"A-0661","document_id":"0b24e8d2-e7cc-4441-abd7-337c8a2c6cf1","item_id":null,"unit_id":4,"service_category_id":"5dc2f6d2-27e0-4dac -a2b7-39080a8e0008","description":"機油濾清器","數量":3,"quantity_partially_received":0,"quantity_partially_shipped":0,"tax":5,"tax2":0,"discount_percent":0 “成本”:24.5,“價格”:28.18,“折扣價格”:0,“重量”:4,“修改”:“2022-12-21T16:01:04 + 00:00”,“創建”:“ 2022-12-15T15:43:57+00:00","item_variations_location_id":null,"item_variation_data":null,"delete":false,"item":null},{"id":"de2c8695-9589- 4856-be7a-d3979a776646","name":"運費","document_id":"0b24e8d2-e7cc-4441-abd7-337c8a2c6cf1","item_id":null,"unit_id":1,"service_category_id":"5e6a78e2 -cf6c-437c-ac52-0c3a0a8e000a","描述":"RE:E2920103","數量":1,"quantity_partially_received":0,"quantity_partially_shipped":0,"tax":5,"tax2" :0,"discount_percent":0,"cost":70.64,"price":95.35,"discounted_price":0,"weight":8,"modified":"2022-12-21T16:01:04+00: 00","created":"2022-12-21T15:34:51+00:00","item_variations_location_id":null,"item_variation_data":null,"delete":false,"item":null},{" id":"eab3c271-20ce-4c4e-af92-8553425eaa71","name":"C-6200","document_id":"0b24e8d2-e7cc-4441-abd7-337c8a2c6cf1","item_id":null,"unit_id" :4,“service_category_id”:“5dc2f6d2-27e0-4dac-a2b7-39080a8e0008”,“description”:“TOP COVER GASKET”,“quantity”:1,“quantity_partially_received”:0,“quantity_partially_shipped”:0,“tax” :5,"tax2":0,"discount_percent":0,"cost":345.85,"price":397.73,"discounted_price":0,"weight":1,"modified":"2022-12-21T16: 01:04+00:00","created":"2022-12-13T21:21:43+00:00","item_variations_location_id":null,"item_variation_data":null,"delete":false,"item" :無效的}]}]}

我想輸出 JSON 內容,使其在我的 google 工作表上看起來像這樣

https://docs.google.com/spreadsheets/d/18x1cyztf5SgnKZHjqgWx2iP7SzQAksOzE_EVZoUOcy8/edit#gid=0

任何幫助將不勝感激謝謝

在您的腳本中,當response.getContentText()的值是您的顯示數據時,如何進行以下修改?

從:

Logger.log(response.getContentText());

到:

Logger.log(response.getContentText());

// I added the below script.
const value = JSON.parse(response.getContentText());
const addValues = [value.document[0].customer.name, value.document[0].issue_date, value.document[0].attention, value.document[0].name];
const res = value.document[0].document_items.map(o => [value.document[0].document_number, ...["name", "description", "quantity", "price", "discounted_price"].map(h => o[h]), ...addValues]);
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
sheet.getRange(sheet.getLastRow() + 1, 1, res.length, res[0].length).setValues(res);
  • 請將工作表名稱設置為getSheetByName("Sheet1")
  • 不幸的是,我無法從您的數據中找到您期望的External PO#

建議:清理並提取所需數據

我注意到的第一件事是您當前的腳本已經從 Salesbinder 中提取了一個 JSON 對象。 但是,返回的格式很難閱讀。 您可以在線使用 JSON 美化網站來重新排列和分析您生成的 JSON 對象,例如JSON Viewer免責聲明:我不隸屬於該網站,我只是覺得由於其有用的功能而將其分享給其他人很有幫助)。

腳本

之后,您可以通過訪問 JSON 對象來訪問您想要的數據。 對於您的表,您可以使用以下內容:

  var salesBinderInvoiceNumVal = object.document[0].document_number;
  var itemNameVal = object.document[0].document_items[0].name;
  var descriptionVal = object.document[0].document_items[0].description;
  var quantityVal = object.document[0].document_items[0].quantity;
  var priceVal = object.document[0].document_items[0].price;
  var discountPercentVal = object.document[0].document_items[0].discount_percent;
  var clientNameVal = object.document[0].customer.name;
  var issueDateVal = object.document[0].issue_date;
  var attentionVal = object.document[0].attention;
  var nameVal = object.document[0].name;

注意:無法找到外部 PO 編號,因為您的示例表中沒有給定值。

但在這樣做之前,您可能希望將生成的 JSON 對象存儲到一個變量中。 因此,您可以更改:

Logger.log(response.getContentText());

到:

var object = response.getContentText();

之后,您可以添加setValues()函數以將值添加到表格的最后一行。 因此,您的腳本應該是這樣的:

function fetching() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Sheet1');
  var sheet2 = ss.getSheetByName('Sheet2');
  var InvNumber = sheet2.getRange('A1').getValue();
  var USERNAME = 'APIKey';
  var PASSWORD = 'x';

  var url = 'https://mydomain.salesbinder.com/api/2.0/documents.json?documentNumber=' + InvNumber + '&contextId=5';
  var headers = {
    "Authorization": "Basic " + Utilities.base64Encode(USERNAME + ':' + PASSWORD)
  };

  let response = UrlFetchApp.fetch(url, { headers });

  //Changes start here
  var object = response.getContentText(); //Store the JSON object to this variable
  var lastRow = sheet.getLastRow(); //get the last row

  //required values
  var salesBinderInvoiceNumVal = object.document[0].document_number;
  var itemNameVal = object.document[0].document_items[0].name;
  var descriptionVal = object.document[0].document_items[0].description;
  var quantityVal = object.document[0].document_items[0].quantity;
  var priceVal = object.document[0].document_items[0].price;
  var discountPercentVal = object.document[0].document_items[0].discount_percent;
  var clientNameVal = object.document[0].customer.name;
  var issueDateVal = object.document[0].issue_date;
  var attentionVal = object.document[0].attention;
  var nameVal = object.document[0].name;

  //add the required values to a 2d array
  var output = [[salesBinderInvoiceNumVal, itemNameVal, descriptionVal, quantityVal, priceVal, discountPercentVal, clientNameVal, issueDateVal, attentionVal, nameVal]];
  sheet.getRange(lastRow+1, 1, 1, output[0].length).setValues(output); //add the 2d array to the last row of your table

}

輸出:

由於您的腳本僅獲取一個單元格的值,因此我假設它僅將一項或一行提取到您的表中。 注意:我只處理了您帖子中給定的 JSON 對象,因為我在 Salesbinder 中沒有訪問權限/帳戶。 我確實假設這是您當前腳本的輸出。

在此處輸入圖像描述

參考:

您可以在本文中進一步研究訪問 JSON 對象日期: JSON Object Literals

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM