因此,我试图做一个简单的任务,就是删除我的谷歌表格中的行。令人惊讶的是,这不是我所期望的工作。因此,从下面的代码中,您可以看到我正在输入我想要删除的所有行号&我记录了控制台日志,以确保这些是我希望它们被删除的行。
for(let i = 0; i < deletion.length; i++)
{
console.log("deleteing " + rowCharacter[deletion[i]].Discord)
await rowCharacter[deletion[i]].delete()
console.log("deleteing " + rowCharacter[deletion[i]].Discord)
}其中一些行最终被删除。有些人不会。任何建议都会很受欢迎,甚至是一条无需循环就可以清除所有内容的命令?我将分享以下方法的全部内容:
async cleanSheet(discordtag:string[]){
await this.memo.loadCells()
let begin = this.memo.rowCount
let rowCharacter = await this.character.getRows()
let deletion:number[] = [], i = 415 , row:GoogleSpreadsheetRow[] = []
console.log("start")
for(let j = 0; i < rowCharacter.length; j++)
{
console.log("checking" + rowCharacter[i].Discord)
if(rowCharacter[i]?.Discord == undefined)
{
i++
}
//console.log( rowCharacter[i].Discord)
else if(discordtag[j] == rowCharacter[i].Discord)
{
i++
j = 0
}
else if(j == discordtag.length)
{
console.log(rowCharacter[i].Discord)
rowCharacter[i].Character = rowCharacter[i].Character + " [retired]"
console.log("rows to add" +rowCharacter[i].Character)
row.push(rowCharacter[i])
//await this.memo.addRow(rowCharacter[i])
deletion.push(i)
i++
j = 0
}
}
await this.memo.addRows(row)
console.log("rows to delete" + deletion)
let end = deletion.length + begin
for(let i = 0; i < deletion.length; i++)
{
console.log("deleteing " + rowCharacter[deletion[i]].Discord)
await rowCharacter[deletion[i]].delete()
console.log("deleteing " + rowCharacter[deletion[i]].Discord)
}
await this.memo.loadCells()
for(let i = begin; i < end; i++)
for( let j = 0; j < 49; j++)
this.memo.getCell(i,j).borders = this.memo.getCell(1,1).borders
this.memo.saveUpdatedCells()
console.log("done")
}发布于 2022-07-15 12:21:37
to delete rows, be careful to start from the end (reverse your loop), otherwise the index of the row would be affected by the previous deletion. –
Mike Steelson迈克·斯蒂尔森谢谢你的回答。你是对的。
https://stackoverflow.com/questions/72988040
复制相似问题