I want to get data from the text found in 2.. In 1. Is the data I have available and in 3. You will find the wanted result.
Categories |
---|
Kleur |
Soorthek |
Bevestigingswijze |
I am getting this text from scraping: BevestigingswijzeKlembevestigingKleurWitSoorthekSpijlenhek
I want this as a result by using a function in Google Sheets.
Wanted Result |
---|
KleurWit |
SoorthekSpijlenhek |
BevestigingswijzeKlembevestiging |
Thank you in advance!
You could accomplish this with an Apps Script custom function. To achieve this, follow these steps:
function splitScrape(categories, scrape) {
const indexes = categories.map(c => scrape.indexOf(c[0])).sort();
const split = indexes.map((index, j) => scrape.slice(index, indexes[j+1]));
return split;
}
The sample above won't detect multiple occurrences of the same category in the scrape string, and it will only handle the first one (if a second parameter is not provided, indexOf only detects the first occurrence). In order to detect multiple occurrences, I'd suggest replacing the function with this one:
function splitScrape(categories, scrape) {
const indexes = [];
categories.flat().filter(String).forEach(c => {
let index = scrape.indexOf(c);
while (index > -1) {
indexes.push(index);
index = scrape.indexOf(c, index + 1);
}
});
indexes.sort((a, b) => a-b);
const split = indexes.map((index, j) => scrape.slice(index, indexes[j+1])).filter(String);
return split;
}
Categories
and the scrape string are located, as you can see here: