如何连接亲爱的库存 API 和 Google sheet?
How to connect Dear inventory APIs and Google sheet?
我想通过使用亲爱的 API 和 Google 工作表将 Dear 库存系统与 Google 工作表连接以导入产品列表、销售列表数据等 API。
它应该会自动更新。
可不可以?
如果是,这里有什么方法吗?
当然可以。有了这个,您就有了一个通用设置,不会处理嵌套对象或数组。但是你可以用你想要的数据创建特定的函数。但是为此你应该雇人,或者自己做。
设置是您可以使用通用函数将您想要的端点获取到您想要的 sheet。 dearAPI(endpoint, sheetname, dig)
Dig:这是保存返回数据数组的对象:
代码:
//GLOBAL variables:
const accountID = 'id';
const secret = 'secret';
function onOpen(e) {
SpreadsheetApp.getUi().createMenu('DEAR')
.addItem('Update all', 'updateAll')
.addToUi();
}
function updateAll(){
dearAPI('product','Products','Products');
dearAPI('salesList','Sales', 'SalesList');
}
/**
* Updates data from specific DEAR endpoint to specific sheet.
*
* @param {string} endpoint - the endpoint the get the data from.
* @param {string} sheetname - name of the sheet to write the data to.
* @param {string} dig - the object where the array of data is returned.
* @return {void}
*/
function dearAPI(endpoint, sheetname, dig) {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(sheetname)
const dataRows = [];
let pagenumber = 1;
do {
const url = `https://inventory.dearsystems.com/externalapi/v2/${endpoint}?page=${pagenumber}&limit=1000`;
const headers = {
"api-auth-accountid": accountID,
"api-auth-applicationkey": secret
};
const options = {
"method" : "get",
"headers" : headers
};
const response = UrlFetchApp.fetch(url, options);
const data = JSON.parse(response.getContentText())
data[dig].forEach(item => dataRows.push(item));
pagenumber++
} while (data.Total != data.Page)
const rowHeaders = Object.keys(dataRows[0]);
const rows = [rowHeaders];
for (let i = 0; i < dataRows.length; i++) {
const rowData = [];
for (let j = 0; j < rowHeaders.length; j++) {
rowData.push(dataRows[i][rowHeaders[j]]);
}
rows.push(rowData);
}
sheet.getRange(1,1,sheet.getLastRow(), sheet.getLastColumn()).clearContent();
sheet.getRange(1,1,rows.length,rows[0].length).setValues(rows);
}
我想通过使用亲爱的 API 和 Google 工作表将 Dear 库存系统与 Google 工作表连接以导入产品列表、销售列表数据等 API。 它应该会自动更新。 可不可以? 如果是,这里有什么方法吗?
当然可以。有了这个,您就有了一个通用设置,不会处理嵌套对象或数组。但是你可以用你想要的数据创建特定的函数。但是为此你应该雇人,或者自己做。
设置是您可以使用通用函数将您想要的端点获取到您想要的 sheet。 dearAPI(endpoint, sheetname, dig)
Dig:这是保存返回数据数组的对象:
代码:
//GLOBAL variables:
const accountID = 'id';
const secret = 'secret';
function onOpen(e) {
SpreadsheetApp.getUi().createMenu('DEAR')
.addItem('Update all', 'updateAll')
.addToUi();
}
function updateAll(){
dearAPI('product','Products','Products');
dearAPI('salesList','Sales', 'SalesList');
}
/**
* Updates data from specific DEAR endpoint to specific sheet.
*
* @param {string} endpoint - the endpoint the get the data from.
* @param {string} sheetname - name of the sheet to write the data to.
* @param {string} dig - the object where the array of data is returned.
* @return {void}
*/
function dearAPI(endpoint, sheetname, dig) {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(sheetname)
const dataRows = [];
let pagenumber = 1;
do {
const url = `https://inventory.dearsystems.com/externalapi/v2/${endpoint}?page=${pagenumber}&limit=1000`;
const headers = {
"api-auth-accountid": accountID,
"api-auth-applicationkey": secret
};
const options = {
"method" : "get",
"headers" : headers
};
const response = UrlFetchApp.fetch(url, options);
const data = JSON.parse(response.getContentText())
data[dig].forEach(item => dataRows.push(item));
pagenumber++
} while (data.Total != data.Page)
const rowHeaders = Object.keys(dataRows[0]);
const rows = [rowHeaders];
for (let i = 0; i < dataRows.length; i++) {
const rowData = [];
for (let j = 0; j < rowHeaders.length; j++) {
rowData.push(dataRows[i][rowHeaders[j]]);
}
rows.push(rowData);
}
sheet.getRange(1,1,sheet.getLastRow(), sheet.getLastColumn()).clearContent();
sheet.getRange(1,1,rows.length,rows[0].length).setValues(rows);
}