- function doPost(e) {
- var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
- var data = JSON.parse(e.postData.contents);
-
- // Suponha que os dados sejam um objeto com chaves correspondentes aos nomes das colunas
- var row = [];
- var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
- for (var i = 0; i < headers.length; i++) {
- var value = data[headers[i]];
- if (typeof value === 'string' || value instanceof String) {
- value = value.replace(/\+/g, '');
- }
- row.push(value);
- }
-
- // Verifique se a linha já existe com base na coluna A
- var lastRow = sheet.getLastRow();
- if (lastRow > 1) {
- var columnA = sheet.getRange(2, 1, lastRow - 1, 1).getValues();
- var rowIndex = -1;
- for (var i = 0; i < columnA.length; i++) {
- if (columnA[i][0] == data[headers[0]]) {
- rowIndex = i + 2;
- break;
- }
- }
-
- // Se a linha existir, atualize-a. Caso contrário, adicione uma nova linha.
- if (rowIndex > -1) {
- sheet.getRange(rowIndex, 1, 1, row.length).setValues([row]);
- } else {
- sheet.appendRow(row);
- }
- } else {
- // Se a planilha estiver vazia, apenas adicione a nova linha
- sheet.appendRow(row);
- }
-
- return ContentService.createTextOutput(JSON.stringify({ 'status': 'success' })).setMimeType(ContentService.MimeType.JSON);
- }
-
-
- function doGet(e) {
- var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
- var columnName = e.parameter.coluna;
- var columnValue = e.parameter.valor;
-
- // Encontre a coluna com base no nome
- var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
- var columnIndex = -1;
- for (var i = 0; i < headers.length; i++) {
- if (headers[i] == columnName) {
- columnIndex = i;
- break;
- }
- }
-
- // Se a coluna não foi encontrada, retorne um erro
- if (columnIndex == -1) {
- return ContentService.createTextOutput(JSON.stringify({ 'status': 'error', 'message': 'Column not found' })).setMimeType(ContentService.MimeType.JSON);
- }
-
- // Busque todas as linhas que correspondem ao valor da coluna
- var data = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn()).getValues();
- var matchingRows = [];
- for (var i = 0; i < data.length; i++) {
- if (data[i][columnIndex] == columnValue) {
- var row = {};
- for (var j = 0; j < headers.length; j++) {
- row[headers[j]] = data[i][j];
- }
- matchingRows.push(row);
- }
- }
-
- // Retorne as linhas correspondentes ou "Não encontrado" se não houver nenhuma
- if (matchingRows.length > 0) {
- return ContentService.createTextOutput(JSON.stringify({ 'status': 'success', 'data': matchingRows })).setMimeType(ContentService.MimeType.JSON);
- } else {
- return ContentService.createTextOutput(JSON.stringify({ 'status': 'Não encontrado', 'data': [] })).setMimeType(ContentService.MimeType.JSON);
- }
- }