function doPost(e) { try { // Open the spreadsheet by URL var spreadsheet = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1HpeYA5dz-K1UDaE05fh8nByUwDH5Vv7zFsB0zam8ebo/edit#gid=0"); // Get the sheet named "Sheet1" var sheet = spreadsheet.getSheetByName("Sheet1"); if (!sheet) { throw new Error('Sheet "Sheet1" not found'); } // Get the contents of the POST request var data = JSON.parse(e.postData.contents); // Check if data is an array or array of arrays if (Array.isArray(data)) { if (!Array.isArray(data[0])) { // If data is a single array, convert it to a 2D array data = [data]; } // Append each row to the sheet sheet.getRange(sheet.getLastRow() + 1, 1, data.length, data[0].length).setValues(data); } else { throw new Error('Invalid data format: Expected an array or array of arrays.'); } // Return a success response return ContentService.createTextOutput("Success"); } catch (error) { // Return an error response return ContentService.createTextOutput('Error: ' + error.message); } } function doGet(e) { try { // Open the active spreadsheet var ss = SpreadsheetApp.getActiveSpreadsheet(); // Get the sheet named 'Sheet1' var sheet = ss.getSheetByName('Sheet1'); // Check if the sheet exists if (!sheet) { throw new Error("Sheet 'Sheet1' not found."); } // Get all data in the sheet var data = sheet.getDataRange().getDisplayValues(); // Convert the data to JSON var jsonData = JSON.stringify(data); // Create text output with specified character encoding (UTF-8 in this case) var output = ContentService.createTextOutput(jsonData) .setMimeType(ContentService.MimeType.JSON) .setContent(jsonData) .setCharset('UTF-8'); return output; } catch (error) { // Return error message as JSON var errorMessage = JSON.stringify({ error: error.message }); // Create text output for the error message with specified character encoding var errorOutput = ContentService.createTextOutput(errorMessage) .setMimeType(ContentService.MimeType.JSON) .setContent(errorMessage) .setCharset('UTF-8'); return errorOutput; } } function clearSheet() { try { // Open the active spreadsheet var ss = SpreadsheetApp.getActiveSpreadsheet(); // Get the sheet named 'Sheet1' var sheet = ss.getSheetByName('Sheet1'); // Check if the sheet exists if (!sheet) { throw new Error("Sheet 'Sheet1' not found."); } // Clear the contents of the sheet sheet.clearContents(); console.log('Sheet cleared successfully.'); } catch (error) { console.error('Error:', error.message); } }