无法使用谷歌电子表格 api v4.0 更新单元格的谷歌工作表值

时间:2021-02-16 07:40:06

标签: javascript node.js discord.js google-sheets-api

我无法更新特定单元格的工作表行值。我正在使用 google api v4.0 和 google 电子表格 npm 包。我在 discord bot js 中使用它时出错。错误是

<块引用>

(node:5956) UnhandledPromiseRejectionWarning: TypeError:rows2[i].save 不是函数 在 checkStatus (D:\discortbot\src\bot.js:95:30)

我的代码如下:

 sheet3 = doc2.sheetsByIndex[sheetIndex]; // Data from back which I have checked is coming correctly!
                   var rows2 = await sheet3.getRows({
                });  
                rows2 = rows2.map(a=>a._rawData);
                var size = rows2.length;
               
                for(var i=0;i<size;i++){
                   if(rows2[i][0]==email){
                    rows2[i].candidateID = userID;
                    rows2[i].RoleID =  sheetarr[2];
                     //rows2[i].save(); This one is also not working
                    console.log(rows2[i].candidateID); // Values are printing in console
                    console.log(rows2[i].RoleID); // Values are  printing in console
                    await rows2[i].save(); // Error giving me
                    verified = 1;
                    break;
                   } 
                    
                }
                if(verified==1){
                    message.author.send("Your account has been verified.Welcome to Outscal batch!");
                }else{

                }

我不明白为什么我会收到这个错误?请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

下面的修改怎么样?

修改后的脚本:

在使用此脚本之前,请再次确认要使用的变量。

var sheet3 = doc2.sheetsByIndex[sheetIndex];
var rows2 = await sheet3.getRows();
// rows2 = rows2.map(a=>a._rawData);  // Removed
var size = rows2.length;

for (var i = 0; i < size; i++) {
  if (rows2[i]._rawData[0] == email) {  // Modified
    rows2[i].candidateID = userID;
    rows2[i].RoleID = sheetarr[2];
    console.log(rows2[i].candidateID); // Values are printing in console
    console.log(rows2[i].RoleID); // Values are  printing in console
    await rows2[i].save();
    verified = 1;
    break;
  }
}
if (verified == 1) {
  message.author.send("Your account has been verified.Welcome to Outscal batch!");
} else {
}

参考: