Published
Edited
Jan 18, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
export_food = async (food_id) => {
const nutrients = await get_nutrients_values(food_id)
const vitamins = nutrients.slice(0, VITAMINS_NAMES.length)
const minerals = nutrients.slice(VITAMINS_NAMES.length)
console.log(vitamins)
console.log(minerals)
const end_vitamins = String.fromCharCode('A'.charCodeAt(0) + VITAMINS_NAMES.length)
const end_minerals = String.fromCharCode('A'.charCodeAt(0) + MINERALS_NAMES.length)
const row = row_num_input
const vitamins_range = `Vitamins!B${row}:${end_vitamins}${row}`
const minerals_range = `Minerals!B${row}:${end_minerals}${row}`
await awaitify_req(gapi.client.sheets.spreadsheets.values.update({
spreadsheetId: SPREADSHEET_ID,
range: vitamins_range,
valueInputOption: "USER_ENTERED",
resource: {
majorDimension: "ROWS",
values: [ vitamins ] }
}))
await awaitify_req(gapi.client.sheets.spreadsheets.values.update({
spreadsheetId: SPREADSHEET_ID,
range: minerals_range,
valueInputOption: "USER_ENTERED",
resource: {
majorDimension: "ROWS",
values: [ minerals ] }
}))
}
Insert cell
get_nutrients_values = async (food_id) => {
const endpoint = `https://api.nal.usda.gov/fdc/v1/${food_id}?api_key=${FDC_KEY}`
const response = await fetch(endpoint).catch(err => { throw err })
const json = await response.json()
if (json.errors)
throw json.errors[0].message
const nutrients = json.foodNutrients
const projection = []
for (const nutrient_name of NUTRIENTS_NAMES) {
let found = false
for (const nutrient_info of nutrients) {
if (nutrient_info.nutrient.name != nutrient_name)
continue
// ideally we'd do a unit check here
projection.push(nutrient_info.amount)
found = true
break
}
if (!found)
projection.push(0)
}
return projection
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more