The following code will enable connection to a google sheet:
Code here
using System; using System.Net.Http; using System.Threading.Tasks; public class GoogleSheetsUpdater { public static async Task Main(string[] args) { string webAppUrl = "https://script.google.com/macros/s/...."; string sheetName = "Sheet1"; int row = 5; int col = 1; string value = "UpdatedValueFromCSharp"; // Construct the full URL with parameters string fullUrl = $"{webAppUrl}?sheetName={sheetName}&row={row}&col={col}&value={Uri.EscapeDataString(value)}"; using (HttpClient client = new HttpClient()) { try { // Send a GET request to the web app URL HttpResponseMessage response = await client.GetAsync(fullUrl); // Ensure the request was successful if (response.IsSuccessStatusCode) { string responseContent = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Google Sheets updated successfully. Response: {responseContent}"); } else { Console.WriteLine($"Error updating Google Sheets. Status code: {response.StatusCode}"); string errorContent = await response.Content.ReadAsStringAsync(); Console.WriteLine($"Error details: {errorContent}"); } } catch (HttpRequestException ex) { Console.WriteLine($"An error occurred during the HTTP request: {ex.Message}"); } } Console.WriteLine("\nPress any key to exit..."); Console.ReadKey(); } }
No comments:
Post a Comment