首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >google sheet API,删除行

google sheet API,删除行
EN

Stack Overflow用户
提问于 2022-07-15 01:00:31
回答 1查看 34关注 0票数 0

因此,我试图做一个简单的任务,就是删除我的谷歌表格中的行。令人惊讶的是,这不是我所期望的工作。因此,从下面的代码中,您可以看到我正在输入我想要删除的所有行号&我记录了控制台日志,以确保这些是我希望它们被删除的行。

代码语言:javascript
复制
 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)    
 }

其中一些行最终被删除。有些人不会。任何建议都会很受欢迎,甚至是一条无需循环就可以清除所有内容的命令?我将分享以下方法的全部内容:

代码语言:javascript
复制
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")

    }
EN

回答 1

Stack Overflow用户

发布于 2022-07-15 12:21:37

代码语言:javascript
复制
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

迈克·斯蒂尔森谢谢你的回答。你是对的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72988040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档