I would like to connect the Dear inventory system with Google Sheets for importing product lists, Sale lists data, etc by using both the Dear API and Google Sheets API.
It should be automatically updated.
Is it possible or not?
If yes, Any methods here?
Sure this is possible. With this you have a generic setup that will NOT handle nested objects or arrays. But you can create specific functions with the data you want. But for that you should hire somebody, or do the work yourself.
The setup is that you can use the generic function to get the endpoint you want to the sheet you want. dearAPI(endpoint, sheetname, dig)
Dig: This is the object that holds the array of the returned data:
The code:
//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);
}
Related
I am trying to change the header names in sheetJS to something I recieve from the form. Here is my form submit function:
onFormSubmit() {
const fileReader = new FileReader();
const solution = 'testsolution'; // normally i get this from a form input
fileReader.onload = (e) => {
this.arrayBuffer = fileReader.result;
const data = new Uint8Array(this.arrayBuffer);
const arr = [];
for (let i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
const bstr = arr.join('');
const workbook = XLSX.read(bstr, {type: 'binary'});
const first_sheet_name = workbook.SheetNames[0];
const worksheet = workbook.Sheets[first_sheet_name];
// change the column names to match entity
const records = XLSX.utils.sheet_to_json(worksheet, {raw: true});
console.log('records - ', records);
};
fileReader.readAsArrayBuffer(this.file);
}
currently for example my headers come in like this:
AltId, FirstName, LastName
And I want them to end up like this with 'testsolution' apended with underscore and lowercase
testsolution_altid, testsolution_firstname, testsolution_lastname
NOTE: Header names may change depending on what sheet is uploaded. For example the next sheet uploaded may contain headers:
AltId, Address1, Address2, City, State
And should end up like
testsolution_altid, testsolution_address1, testsolution_address2, testsolution_city, testsolution_state
Any help would be appriciated!
I ended up doing it like this if it helps anyone else:
// change the column names to match entity
const newHeaders = [];
const columnCount = XLSX.utils.decode_range(ws['!ref']).e.c +1;
console.log(columnCount);
for (let i = 0; i < columnCount; ++i) {
newHeaders[i] = solution+'_'+ ws[`${XLSX.utils.encode_col(i)}1`].v.toLowerCase();
// console.log(header[i]);
}
// console.log(newHeaders);
I would like to add different API request on a spreadsheet where i already add some google app script code from here in oder to retrieve all orders from a woocommerce external source, however now i want to add another API calling request which allows me to list all products on my woocommerce source. So i type a new function below, modify the endpoint to /wp-json/wc/v3/products according to woocommerce API rest documentation here Woocommerce API rest doc. Here is the code i add to the Github code :
// Custom code to v2 Woocommerce API
function start_syncv2() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(Products);
fetch_products(sheet)
}
function fetch_products(sheet) {
var ck = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(OrderDetails).getRange("B4").getValue();
var cs = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(OrderDetails).getRange("B5").getValue();
var website = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(OrderDetails).getRange("B3").getValue();
var surl = website + "/wp-json/wc/v3/sheet?consumer_key=" + ck + "&consumer_secret=" + cs + "&after=" + "&per_page=100";
var url = surl
Logger.log(url)
var options =
{
"method": "GET",
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
"muteHttpExceptions": true,
};
var result = UrlFetchApp.fetch(url, options);
Logger.log(result.getResponseCode())
if (result.getResponseCode() == 200) {
var params = JSON.parse(result.getContentText());
Logger.log(result.getContentText());
}
var doc = SpreadsheetApp.getActiveSpreadsheet();
var temp = SpreadsheetApp.getSheetByName(sheet);
var consumption = {};
var arrayLength = params.length;
Logger.log(arrayLength)
for (var i = 0; i < arrayLength; i++) {
Logger.log("dfsfsdfsf")
var a;
var container = [];
a = container.push(params[i]["id"]);
Logger.log(a)
a = container.push(params[i]["name"]);
a = container.push(params[i]["sku"]);
a = container.push(params[i]["price"]);
a = container.push(params[i]["tax_status"]);
a = container.push(params[i]["stock_status"]);
a = container.push(params[i]["categories"]["name"]);
a = container.push(params[i]["images"]);
a = container.push(params[i]["attributes"]["options"]);
a = container.push(params[i]["_links"]["self"]["href"]);
var doc = SpreadsheetApp.getActiveSpreadsheet();
var temp = doc.getSheetByName(sheet);
temp.appendRow(container);
}
}
I have issue using google sheets calling method describe here. As i want my new API call request, i think my main problem is to select the right sheet into the spreadsheet.
Here is a copy of my spreadsheet : Woocommerce-google sheets integration
I want to retrieve data via Jenkins API using Google Sheet Script and store it in Google Sheet
1) Pull Jenkins Job Builds using Jenkins API to Google Sheet - DONE
2) Store data to Google Sheet ???
(need only "builds.subBuilds.buildNumber" and "builds.subBuilds.duration" values)
(need to correct mistake in the script)
function getJenkinsBuilds() {
// get the jenkins job
var response = UrlFetchApp.fetch('http://jenkins.[domain].co/job/Build+Deploy/api/json', {
'method': 'get',
'muteHttpExceptions' : true,
'headers' : {'Authorization' : 'Basic [tokan]'},
});
// parse the json reply and return builds
var data = JSON.parse(response);
var builds = data["builds"];
Logger.log(builds);
return builds;
};
// store predefined parameters from builds in the spreadsheet
function setDataToTable() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Jenkins');
var cell = sheet.getRange("A1");
var rows = [['buildNumber','duration'],['','']]; // I GUESS THE MISTAKE IS HERE?
sheet.getRange(cell.getRow(), cell.getColumn(), rows.length, rows[0].length).setValues(rows);
}
Actual result:
Log shows retrieved array with Builds objects, i.e.:
[19-10-10 16:18:16:937 AEDT] [{number=2081, subBuilds=[{jobName=...
'Jenkins' spreadsheet is empty.
Expected result:
Store "builds.subBuilds.buildNumber" and "builds.subBuilds.duration" values
in the Google Sheet ('Jenkins' spreadsheet), i.e.:
buildNumber duration
123 15sec
456 16sec
... ...
I was able to make it working in the next way:
function getJenkinsBuilds()
{
// get jenkins builds
var response = UrlFetchApp.fetch('http://jenkins.[domain].co/job/Build+Deploy/api/json', {
'method': 'get',
'muteHttpExceptions' : true,
'headers' : {'Authorization' : 'Basic [token]'}
});
// parse the json reply
var data = JSON.parse(response);
var builds = data["builds"];
var number = data['builds'][0]['number'];
var url = data['builds'][0]['url'];
Logger.log(number);
Logger.log(url);
// fill in the spreadsheet with data
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Jenkins');
var cell = sheet.getRange('A1');
var rows = [['BUILD']];
for (var i = 0; i < builds.length; i++)
{
var number = data['builds'][i]['number'];
var url = data['builds'][i]['url'];
rows.push(['=HYPERLINK("'+url+'","'+number+'")']);
Logger.log(number);
Logger.log(url);
sheet.getRange(cell.getRow(), cell.getColumn(), rows.length, rows[0].length).setValues(rows);
}
};
I am using react native async storage it works good but in some cases, I have to set an expiration date for data and refresh my storage I checked
AsyncStorage documentation but there are no options to set expire after a specific time.
only available options are:-
AsyncStorage.removeItem
AsyncStorage really only handles storage and nothing beyond that.
If you want to set an expiration, just put a key in your data for access date and set it to new Date(). Then, when you pull data, do a date check on the expiration key based on when it should expire.
first, I am storing objects, not strings so my solution will be based on object case if anyone uses strings he can append expireAt the object key then he will extract expire date and compare it with the current date
my solution:-
/**
*
* #param urlAsKey
* #param expireInMinutes
* #returns {Promise.<*>}
*/
async getCachedUrlContent(urlAsKey, expireInMinutes = 60) {
let data = null;
await AsyncStorage.getItem(urlAsKey, async (err, value) => {
data = (JSON.parse(value));
// there is data in cache && cache is expired
if (data !== null && data['expireAt'] &&
new Date(data.expireAt) < (new Date())) {
//clear cache
AsyncStorage.removeItem(urlAsKey);
//update res to be null
data = null;
} else {
console.log('read data from cache ');
}
});
//update cache + set expire at date
if (data === null) {
console.log('cache new Date ');
//fetch data
data = fetch(urlAsKey).then((response) => response.json())
.then(apiRes => {
//set expire at
apiRes.expireAt = this.getExpireDate(expireInMinutes);
//stringify object
const objectToStore = JSON.stringify(apiRes);
//store object
AsyncStorage.setItem(urlAsKey, objectToStore);
console.log(apiRes.expireAt);
return apiRes;
});
}
return data;
},
/**
*
* #param expireInMinutes
* #returns {Date}
*/
getExpireDate(expireInMinutes) {
const now = new Date();
let expireTime = new Date(now);
expireTime.setMinutes(now.getMinutes() + expireInMinutes);
return expireTime;
}
You can use this also, improvement from Ahmed Farag Mostafa answers
import AsyncStorage from "#react-native-async-storage/async-storage";
export default class ExpireStorage {
static async getItem(key) {
let data = await AsyncStorage.getItem(key);
data = JSON.parse(data);
if (
data !== null &&
data.expireAt &&
new Date(data.expireAt) < new Date()
) {
await AsyncStorage.removeItem(key);
data = null;
}
return data?.value;
}
static async setItem(key, value, expireInMinutes) {
const data = { value };
if (expireInMinutes) {
const expireAt = this.getExpireDate(expireInMinutes);
data.expireAt = expireAt;
} else {
const expireAt = JSON.parse(await AsyncStorage.getItem(key))?.expireAt;
if (expireAt) {
data.expireAt = expireAt;
} else {
return;
}
}
const objectToStore = JSON.stringify(data);
return AsyncStorage.setItem(key, objectToStore);
}
static async removeItem(key) {
return AsyncStorage.removeItem(key);
}
static getExpireDate(expireInMinutes) {
const now = new Date();
const expireTime = new Date(now);
expireTime.setMinutes(now.getMinutes() + expireInMinutes);
return expireTime;
}
}
I am looping through an object however in the asynchronous part the i variable is always five.
How can I maintain that value, or pass it into the function
getProductData: function() {
var vm = this;
for (var i = 0; i < vm.recommendationResponse['recommendedItems'].length; i++) {
var sku = vm.recommendationResponse['recommendedItems'][i]['items'][0]['id'];
vm.$http.get('http://127.0.0.1:8000/models/api/productimage/' + sku).then(response => {
// get body data
vm.recommendationResponse['recommendedItems'][i]['items'][0]['image_url'] = response.body['product_image_url'];
vm.recommendationResponse['recommendedItems'][i]['items'][0]['price'] = response.body['price'];
}, response => {
vm.recommendationResponse['recommendedItems'][i]['items'][0]['image_url'] = '';
vm.recommendationResponse['recommendedItems'][i]['items'][0]['price'] = '';
});
}
}
I I do something like this:
vm.$http.get('http://127.0.0.1:8000/models/api/productimage/' + sku).then((response, i) => ...
then i is undefined
Who do I keep the index of the loop or should I go about it a different way?
Always use let to initialize variables in for loop when dealing with async operations. Similar things goes to having for loops in intervals. By using let you make sure you always have a unique variable assigned to i.
for (let i = 0, recommendationlength = vm.recommendationResponse['recommendedItems'].length; i < recommendationlength; i++)
Little bonus, if you cache array length in the beginning you get a small performance boost :-)
You could use Array.prototype.forEach instead:
var vm = this;
vm.recommendataionResponse['recommendedItems'].forEach((item, i) => {
var sku = vm.recommendationResponse['recommendedItems'][i]['items'][0]['id'];
vm.$http.get('http://127.0.0.1:8000/models/api/productimage/' + sku).then(response => {
// get body data
vm.recommendationResponse['recommendedItems'][i]['items'][0]['image_url'] = response.body['product_image_url'];
vm.recommendationResponse['recommendedItems'][i]['items'][0]['price'] = response.body['price'];
}, response => {
vm.recommendationResponse['recommendedItems'][i]['items'][0]['image_url'] = '';
vm.recommendationResponse['recommendedItems'][i]['items'][0]['price'] = '';
});
})
This way, since there is a unique scope for each i value, each .then callback will be able to reference the correct value.