Answer the question
In order to leave comments, you need to log in
What cycles should it take to go through the array in order to be able to add the missing elements?
It was necessary to get Google Spreadsheet data in the frontend. It worked out.
The table looks like this:
https://docs.google.com/spreadsheets/d/1371WG6WudE...
But when you request the json of this table, Google gives you this stuffing:
https://spreadsheets.google.com/feeds/cells/ 1371WG...
OK. If we attach to the event response that comes with an AJAX request and access srcElement.response.feed.entry, then we will get an array with objects. And each object corresponds to a cell in the table.
Here is an example
var request = new XMLHttpRequest();
request.responseType = "json";
request.open("GET", "https://spreadsheets.google.com/feeds/cells/1371WG6WudEXfukqMMakOTJSK_P33cOyXOG69LoN8tyg/1/public/full?alt=json");
request.onload = (req) => {
console.log(req.srcElement.response.feed.entry)
}
request.send();
var cards = [
{id: "1", english: "Some English sentence, if there is one", translation: "Перевод, если он есть", video: "01", video_format: ".mp4"}
]
Answer the question
In order to leave comments, you need to log in
For example like this:
var result = [];
var request = new XMLHttpRequest();
request.responseType = "json";
request.open("GET", "https://spreadsheets.google.com/feeds/cells/1371WG6WudEXfukqMMakOTJSK_P33cOyXOG69LoN8tyg/1/public/full?alt=json");
request.onload = (req) => {
let gscell = req.srcElement.response.feed.entry.map( (e) => e.gs$cell);
let col_row = parseInt(gscell[gscell.length-1].row)+1;
let i = 1;
while (i < col_row) {
let res_row = gscell.filter( e => parseInt(e.row) == i);
this_row = {};
this_row.id = ( res_row.filter( e => parseInt(e.col) == 1).length) ? res_row.filter( e => parseInt(e.col) == 1)[0].inputValue : null;
this_row.english = ( res_row.filter( e => parseInt(e.col) == 2).length) ? res_row.filter( e => parseInt(e.col) == 2)[0].inputValue : null;
this_row.translation = ( res_row.filter( e => parseInt(e.col) == 3).length) ? res_row.filter( e => parseInt(e.col) == 3)[0].inputValue : null;
this_row.video = ( res_row.filter( e => parseInt(e.col) == 4).length) ? res_row.filter( e => parseInt(e.col) == 4)[0].inputValue : null;
this_row.video_format = ( res_row.filter( e => parseInt(e.col) == 5).length) ? res_row.filter( e => parseInt(e.col) == 5)[0].inputValue : null;
result.push(this_row);
i++;
}
console.log(result);
}
request.send();
Interestingly, the wrong question leads to such complicated consequences.
What you are using is the deprecated Google Data API . You also need to use the Google Sheets API.
The code
const getUrl = (id, range, key) =>
`https://sheets.googleapis.com/v4/spreadsheets/${id}/values/${range}?key=${key}`;
const arrToCollect = (array) =>
array
.slice(1)
.map(
(_, ri) =>
array[0].reduce((ah, h, ci) => ((ah[h] = array[ri + 1][ci]), ah), {}),
[]
);
(async () => {
const id = '1xTXNtfabGIiFR9PdOQonmnlSPbhGcj_2Geo1v0cq4Gw';
const range = 'Sheet';
// Limited to the use of the contributor.pw/* domain
const key = 'AIzaSyCt4F7Z8cVDqivNcO3slXewThZurJ4gJNY';
let res = {};
try {
res = await fetch(getUrl(id, range, key));
const data = JSON.parse(await res.text());
console.log(data.values);
console.log(arrToCollect(data.values));
} catch (err) {
console.error(err);
}
})();
getUrl
- a function for forming the address of the request to the API. arrToCollect
- function of converting a two-dimensional array into an array of objects. I just copy it, that's why it's so dense. Details https://github.com/contributorpw/google-apps-scrip... Unfortunately, due to blocking, I can't send you a direct link to the site. See (if not too lazy) https://apps-script-snippets.contributor (dot) pw/snippets/common_js/2darray_to_collection/ Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question