1. function doPost(e) {
  2. var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  3. var data = JSON.parse(e.postData.contents);
  4. // Suponha que os dados sejam um objeto com chaves correspondentes aos nomes das colunas
  5. var row = [];
  6. var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
  7. for (var i = 0; i < headers.length; i++) {
  8. var value = data[headers[i]];
  9. if (typeof value === 'string' || value instanceof String) {
  10. value = value.replace(/\+/g, '');
  11. }
  12. row.push(value);
  13. }
  14. // Verifique se a linha já existe com base na coluna A
  15. var lastRow = sheet.getLastRow();
  16. if (lastRow > 1) {
  17. var columnA = sheet.getRange(2, 1, lastRow - 1, 1).getValues();
  18. var rowIndex = -1;
  19. for (var i = 0; i < columnA.length; i++) {
  20. if (columnA[i][0] == data[headers[0]]) {
  21. rowIndex = i + 2;
  22. break;
  23. }
  24. }
  25. // Se a linha existir, atualize-a. Caso contrário, adicione uma nova linha.
  26. if (rowIndex > -1) {
  27. sheet.getRange(rowIndex, 1, 1, row.length).setValues([row]);
  28. } else {
  29. sheet.appendRow(row);
  30. }
  31. } else {
  32. // Se a planilha estiver vazia, apenas adicione a nova linha
  33. sheet.appendRow(row);
  34. }
  35. return ContentService.createTextOutput(JSON.stringify({ 'status': 'success' })).setMimeType(ContentService.MimeType.JSON);
  36. }
  37. function doGet(e) {
  38. var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  39. var columnName = e.parameter.coluna;
  40. var columnValue = e.parameter.valor;
  41. // Encontre a coluna com base no nome
  42. var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
  43. var columnIndex = -1;
  44. for (var i = 0; i < headers.length; i++) {
  45. if (headers[i] == columnName) {
  46. columnIndex = i;
  47. break;
  48. }
  49. }
  50. // Se a coluna não foi encontrada, retorne um erro
  51. if (columnIndex == -1) {
  52. return ContentService.createTextOutput(JSON.stringify({ 'status': 'error', 'message': 'Column not found' })).setMimeType(ContentService.MimeType.JSON);
  53. }
  54. // Busque todas as linhas que correspondem ao valor da coluna
  55. var data = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn()).getValues();
  56. var matchingRows = [];
  57. for (var i = 0; i < data.length; i++) {
  58. if (data[i][columnIndex] == columnValue) {
  59. var row = {};
  60. for (var j = 0; j < headers.length; j++) {
  61. row[headers[j]] = data[i][j];
  62. }
  63. matchingRows.push(row);
  64. }
  65. }
  66. // Retorne as linhas correspondentes ou "Não encontrado" se não houver nenhuma
  67. if (matchingRows.length > 0) {
  68. return ContentService.createTextOutput(JSON.stringify({ 'status': 'success', 'data': matchingRows })).setMimeType(ContentService.MimeType.JSON);
  69. } else {
  70. return ContentService.createTextOutput(JSON.stringify({ 'status': 'Não encontrado', 'data': [] })).setMimeType(ContentService.MimeType.JSON);
  71. }
  72. }