使用 c# 通过 Google Sheets API v4 检查 Google Sheets 中的空单元格

Checking for Empty Cells in Google Sheets using c# via Google Sheets API v4

这是代码片段。我已经使用 C# 成功连接到 Google Sheets API v4。我发现当一个单元格是空白或空的时候。我收到此错误:System.ArgumentOutOfRangeException:'索引超出范围。必须为非负数且小于集合的大小。 参数名称:index' 如您所见,E3 单元格中的输入 ID 为 blank/empty。我在下面的代码中试过这个来检查一旦它是空白它给出零值,但仍然给出相同的错误。我想要实现的是,一旦单元格为空,我就给它赋零值或未知值??请记住,如果我将值放入单元格 E3 中,代码工作正常,只需要检查空单元格。从这里参考 https://developers.google.com/sheets/api/quickstart/dotnet#further_reading

            int inputId = 0;
            foreach (var row in values)
            {

                SqlCommand command = new SqlCommand();
                command.Connection = ServerConnection;            // <== lacking
                command.CommandType = CommandType.Text;
                command.CommandText = "INSERT into [GoogleSheetsAPI].[dbo].[Followup Stage] ( [Timestamp], [Email Address], [Do You Want To Follow Up] ,[Is The Issue Resolved From Follow Up] ,[Input ID]) VALUES (@TimeStamp, @EmailAddress,  @DoYouWantToFollowUp,  @IsTheIssueResolvedFromFollowUp,  @InputID ) ";
                command.Parameters.AddWithValue("@TimeStamp", Convert.ToDateTime(row[0]));
                command.Parameters.AddWithValue("@EmailAddress", Convert.ToString(row[1]));
                command.Parameters.AddWithValue("@DoYouWantToFollowUp", Convert.ToString(row[2]));
                command.Parameters.AddWithValue("@IsTheIssueResolvedFromFollowUp", Convert.ToString(row[3]));
 //checking for null   or if the cell is empty  still doesn't work this way??
                if (row[4] == null || string.IsNullOrEmpty(Convert.ToString(row[4])))
                {
                    inputId = 0;
                }
                else
                {
                    inputId = Convert.ToInt32(row[4]);
                }

                command.Parameters.AddWithValue("@InputID", inputId);


                try
                {
                    // ServerConnection.Open();
                    int recordsAffected = command.ExecuteNonQuery();
                }
                catch (SqlException E)
                {
                    // error here
                    Console.Write(E.Message);
                    //   Console.ReadLine;
                }



            }

我们必须了解的一件事是 Sheets API 不读取 "empty cells"。您可以在 Read a single range grouped by column:

中阅读

Empty trailing rows and columns are omitted from the response.

它可能不是直接的,但这也是测试所说的。当我读取一个空单元格时,我得到同样的错误

Uncaught TypeError: Cannot read property 'length' of undefined

一个解决方法是为 "empty cells" 编写一个占位符,就像您对 "zero or unknown" 所做的一样。

如果这不是一个选项,您还可以执行类似 if-else 的方法来在程序停止之前捕获错误。要实现这一点,请执行以下操作:

示例:

 for (i = 0; i < range.values.length; i++) {
        //IF NOT AN EMPTY CELL
        if(range.values[i][j]){

           console.log(range.values[i][i] + " is my value");
         //IF IT's AN EMPTY CELL
         }else{

            console.log("U N D E F I N E D");
            //your method to write to that cell
            writeToSheet();
         }         
  }

希望这个简单的类比对您有所帮助。