无法在 9 之后循环浏览谷歌表格(返回 1 然后返回 0)



我有一点麻烦设置一个动态谷歌表来获取数据。如果选项值大于10,它将不起作用;代码以单独的1和0的形式返回值。依此类推,值11就是1和1

任何帮助将是了不起的,非常感谢你!

<select name="examples" id="selectEvents" class="my-events">
<option value="" placeholder="select" selected>Select</option>
</select>
<h2 class="event-name" ></h2>
<h3 class="event-info" ></h3>
<script>
//variables for spreadsheet
const eventName = document.querySelector('.event-name');
const eventInfo = document.querySelector('.event-info');
//variables for sheet
const sheetId = '1wJp22oIlG29X6FDszaPoh71IvbwrZYoWRqwwYy9gbQg';
const base = `https://docs.google.com/spreadsheets/d/${sheetId}/gviz/tq?`;
const sheetName = 'sheet1';
const query = encodeURIComponent('Select *');
const url = `${base}&sheet=${sheetName}&tq=${query}`;
fetch(url)
.then(res => res.text())
.then(rep => {
//Remove additional text and extract only JSON:
const jsonData = JSON.parse(rep.substring(47).slice(0, -2));
const dataRows = jsonData.table.rows;
const select = document.getElementById('selectEvents');
for (const r of dataRows) {
select.options[select.options.length] = new Option(r.c[1].v, Number(select.options.length + 1));
}
const selectElement = document.querySelector('.my-events');
selectElement.addEventListener('change', event => {
for (let i of Object.values(selectEvents.value)) {
if (selectEvents.value === i) {
eventName.textContent = jsonData.table.rows[i - 2].c[1].v;
eventInfo.textContent = jsonData.table.rows[i - 2].c[2].v;
}
}
});
});
</script>

可以,尝试+select.options.length + 1通过在select.options.length前面添加+

select.options[select.options.length] = new Option(r.c[1].v, Number(+select.options.length + 1));

相关内容

  • 没有找到相关文章

最新更新