Created
August 28, 2023 06:33
-
-
Save marcodestefano/7a8b47514431584ba407f894f2129af2 to your computer and use it in GitHub Desktop.
Import Curve card data into a Google Sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function doPost(e) { | |
var output = null; | |
try { | |
var requestData = JSON.parse(e.postData.contents); | |
var workbook_id = requestData.sheetID; | |
var contentValue = requestData.content; | |
var EXPENSE_SHEET_NAME = "New_Expenses"; | |
var workbook = SpreadsheetApp.openById(workbook_id); | |
var sheet = workbook.getSheetByName(EXPENSE_SHEET_NAME); | |
if (!sheet) { | |
sheet = workbook.insertSheet(EXPENSE_SHEET_NAME, workbook.getSheets().length); | |
} | |
else { | |
sheet.deleteRows(1, sheet.getLastRow()-1); | |
} | |
var rows = contentValue.split("\n").filter(row => row.trim() !== ""); | |
if (rows.length > 0) { | |
var numColumns = rows[0].split(";").length; | |
var range = sheet.getRange(1, 1, rows.length, numColumns); | |
range.setValues(rows.map(row => row.split(";"))); | |
range.createFilter(); | |
} | |
output = buildOutput(200,"✅ Caricamento di "+ rows.length + " righe completato!") | |
} | |
catch(error) { | |
output = buildOutput(400,"😡 Errore nel caricamento: " + error.message); | |
} | |
finally { | |
return output; | |
} | |
} | |
function buildOutput(status, message) { | |
var response = { | |
status: status, | |
message: message | |
}; | |
var output = ContentService.createTextOutput(JSON.stringify(response)); | |
output.setMimeType(ContentService.MimeType.JSON); | |
return output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment