Вывести всю информацию из xml-файл
Всем привет! надеюсь на вашу помощь :)
Есть xml-файл на 5000 строк, например такого вида:
<slugs>
<slug>
<storyId>591921840</storyId>
<storyTitle>JN_MDG_SHAPKA</storyTitle>
<storyStatus>7105</storyStatus>
<storyDur>00:00:11:24</storyDur>
<storyCreated>03/60/2023 16:00:10</storyCreated>
<storyCreatedBy>Server</storyCreatedBy>
<storyModified>03/60/2023 16:00:10</storyModified>
<storyModifiedBy>Server</storyModifiedBy>
<storyCustom name="col1" label="NLE"></storyCustom>
<storyCustom name="col2" label="Test1"></storyCustom>
<storyCustom name="col3" label="Test2 ">false</storyCustom>
<storyCustom name="col4" label="Test3 ">false</storyCustom>
<storyCustom name="col5" label="Test4"></storyCustom>
<storyCustom name="col6" label="Test5"></storyCustom>
<storyCustom name="col7" label="taskNumbers">12</storyCustom>
<slugId>591912579</slugId>
<script>
<main>
<body>
<element>
<elementId>2</elementId>
<elementType>Good</elementType>
<elementDur>00:00:11:24</elementDur>
<clip>
<clipName>JN_MDG_SHAPKA</clipName>
<clipMosId>stratus</clipMosId>
<clipObjId>00008RIO</clipObjId>
<clipFilePath></clipFilePath>
<clipIn></clipIn>
<clipOut></clipOut>
</clip>
<text></text>
<commands/>
</element>
</body>
</main>
</script>
</slug>
<slugs>
<slug>
<storyId>591951842</storyId>
<storyTitle>JN_MDG_SHAPKA2</storyTitle>
<storyStatus>7105</storyStatus>
<storyDur>00:00:11:24</storyDur>
<storyCreated>03/60/2023 16:00:10</storyCreated>
<storyCreatedBy>Server</storyCreatedBy>
<storyModified>03/60/2023 16:00:10</storyModified>
<storyModifiedBy>Server</storyModifiedBy>
<storyCustom name="col1" label="NLE"></storyCustom>
<storyCustom name="col2" label="Test1"></storyCustom>
<storyCustom name="col3" label="Test2 ">false</storyCustom>
<storyCustom name="col4" label="Test3 ">false</storyCustom>
<storyCustom name="col5" label="Test4"></storyCustom>
<storyCustom name="col6" label="Test5"></storyCustom>
<storyCustom name="col7" label="taskNumbers">84623</storyCustom>
<slugId>591912579</slugId>
<script>
<main>
<body>
<element>
<elementId>2</elementId>
<elementType>Good</elementType>
<elementDur>00:00:11:24</elementDur>
<clip>
<clipName>JN_MDG_SHAPKA2</clipName>
<clipMosId>stratus</clipMosId>
<clipObjId>00008RIO</clipObjId>
<clipFilePath></clipFilePath>
<clipIn></clipIn>
<clipOut></clipOut>
</clip>
<text></text>
<commands/>
</element>
</body>
</main>
</script>
</slug>
<slugs>
<slug>
<storyId>591921840</storyId>
<storyTitle>JN_MDG_SHAPKA343</storyTitle>
<storyStatus>7105</storyStatus>
<storyDur>00:00:11:24</storyDur>
<storyCreated>03/60/2023 16:00:10</storyCreated>
<storyCreatedBy>Server</storyCreatedBy>
<storyModified>03/60/2023 16:00:10</storyModified>
<storyModifiedBy>Server</storyModifiedBy>
<storyCustom name="col1" label="NLE"></storyCustom>
<storyCustom name="col2" label="Test1"></storyCustom>
<storyCustom name="col3" label="Test2 ">false</storyCustom>
<storyCustom name="col4" label="Test3 ">false</storyCustom>
<storyCustom name="col5" label="Test4"></storyCustom>
<storyCustom name="col6" label="Test5"></storyCustom>
<storyCustom name="col7" label="taskNumbers"></storyCustom>
<slugId>591912579</slugId>
<script>
<main>
<body>
<element>
<elementId>2</elementId>
<elementType>Good</elementType>
<elementDur>00:00:11:24</elementDur>
<clip>
<clipName>JN_MDG_SHAPKA343</clipName>
<clipMosId>stratus</clipMosId>
<clipObjId>00008RIO</clipObjId>
<clipFilePath></clipFilePath>
<clipIn></clipIn>
<clipOut></clipOut>
</clip>
<text></text>
<commands/>
</element>
</body>
</main>
</script>
</slug>
С помощью гугл-таблицы необходимо вывести на отдельный лист вывести всю информацию только из тех блоков, где встречается номер в taskNumbers
, между тегами storyCustom name="col7" label="taskNumbers
и storyCustom
.
То есть, например, если вставить в таблицу данные, которые я написал выше, ответ должен быть следующий:
Найдено количество taskNumbers: 2 шт. (12, 84623)
taskNumbers: 12
storyId: 591921840
storyTitle: JN_MDG_SHAPKA
storyStatus: 7105
storyDur: 00:00:11:24
storyCreated: 03/60/2023 16:00:10
storyCreatedBy: Server
storyModified: 03/60/2023 16:00:10
storyModifiedBy: Server
NLE:
Test1:
Test2:
Test3:
Test4:
Test5:
slugId: 591912579
taskNumbers: 84623
storyId: 591951842
storyTitle: JN_MDG_SHAPKA2
storyStatus: 7105
storyDur: 00:00:11:24
storyCreated: 03/60/2023 16:00:10
storyCreatedBy: Server
storyModified: 03/60/2023 16:00:10
storyModifiedBy: Server
NLE:
Test1:
Test2:
Test3:
Test4:
Test5:
slugId: 591912579
что мне удалось сделать:
function XML() {
const ss = SpreadsheetApp.openById('xxx');
const list_xml = ss.getSheetByName('xml');
const data = list_xml.getDataRange().getValues();
const taskNumbers = [];
let count = 0;
list_xml.getRange('E2:Q4').clearContent();
for (let i = 0; i < data.length; i++) {
const row = data[i];
for (let j = 0; j < row.length; j++) {
const cell = row[j];
if (cell.indexOf('<storyCustom') !== -1 && cell.indexOf('name="col7"') !== -1) {
const labelStartIndex = cell.indexOf('label="taskNumbers"');
if (labelStartIndex !== -1) {
const contentStartIndex = cell.indexOf('>', labelStartIndex) + 1;
const contentEndIndex = cell.indexOf('</storyCustom>', contentStartIndex);
const taskNumber = cell.substring(contentStartIndex, contentEndIndex).trim();
if (taskNumber !== '') {
taskNumbers.push(taskNumber);
count++;
}
}
}
}
}
if (count > 0) {
const result = `найдено количество taskNumbers: ${count} шт. (${taskNumbers.join(', ')})`;
Logger.log(result);
list_xml.getRange('E2:Q4').setValue(result);
} else {
Logger.log('taskNumbers не найдено.');
list_xml.getRange('E2:Q4').setValue('taskNumbers не найдено.');
}
}
- открываем электронную таблицу Google Sheets по ее идентификатору;
- получает список значений (данных) со страницы "xml";
- проходим по каждому элементу из списка данных и ищем строки со специфическим текстом (содержащим
<storyCustom
иname="col7"
); - если найдена строка с таким текстом, она ищет подстроку с меткой
label="taskNumbers"
и получает номер задания, который находится между тегами<storyCustom>
и</storyCustom>
; - если номер задания не пуст, она добавляет его в список номеров задач;
- после прохождения всех элементов, функция проверяет, был ли найден хотя бы один номер задания;
- если номера задач были найдены, функция устанавливает значение в ячейке "E3" с результатом, содержащим количество найденных редакционных задач и их номера;
- если номера задач не были найдены, функция устанавливает значение в ячейке "E3" со строкой, сообщающей, что редакционные задачи не найдены;
в каком направлении дальше двигаться? :)