Untitled

mail@pastecode.io avatarunknown
plain_text
a month ago
3.3 kB
4
Indexable
Never
const sheets = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1lDR9OwZkH1gs3RuKik3ZDOdOQPyo9cBNIVGTTs0DmHk/edit#gid=0");
const sheet = sheets.getSheetByName("Tickets");

function doPost(e) {
  let data = e.parameter;

  // Handle file attachment
  let attachmentUrl = '';
  if (data.attachment) {
    const attachmentBlob = Utilities.newBlob(data.attachment, data.attachmentType, 'attachment' + data.extension);
    const folder = DriveApp.getFolderById('1cBFwE9fAsjq2_NOrtRKWQUsy85ZEE7cp'); // Replace with the appropriate Google Drive folder ID
    const file = folder.createFile(attachmentBlob);
    attachmentUrl = file.getUrl();
  } else {
    attachmentUrl = 'N/A';
  }

  sheet.appendRow([
    data.ticketID, data.ticketType, data.severity, data.ticketDescription, data.ticketStatus,
    data.correctiveAction, data.comments, data.impactedTechnology, data.appImpacted,
    data.appReboot, data.configChanges, data.recoveryActions, attachmentUrl
  ]);

  return ContentService.createTextOutput("Your ticket was successfully saved to the Google Sheets database");
}

function getSheetData() {
  var values = sheet.getDataRange().getValues();
  var headerRow = values[0];
  var data = [];

  for (var i = 1; i < values.length; i++) {
    var rowData = {};
    for (var j = 0; j < headerRow.length; j++) {
      rowData[headerRow[j]] = values[i][j];
    }
    data.push(rowData);
  }

  return data;
}

function doGet(e) {
  if (e.parameter.action === 'getSheetData') {
    var sheetData = getSheetData();
    return ContentService.createTextOutput(JSON.stringify(sheetData))
      .setMimeType(ContentService.MimeType.JSON);
  }
}

function updateTicketStatus(data) {
  const rowIndex = data.rowIndex;
  const newStatus = data.newStatus;

  if (rowIndex > 0 && rowIndex <= sheet.getLastRow()) {
    sheet.getRange(rowIndex + 1, getColumnIndex("Ticket Status") + 1).setValue(newStatus);
    return true;
  } else {
    return false;
  }
}

function getColumnIndex(columnName) {
  const headerRow = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0];
  const columnIndex = headerRow.indexOf(columnName);
  return columnIndex;
}

function doUpdateTicketStatus(e) {
  if (e.method === "OPTIONS") {
    return ContentService.createTextOutput("")
      .setMimeType(ContentService.MimeType.TEXT)
      .setHeaders({
        'Access-Control-Allow-Origin': 'http://localhost:3000', // Replace with your frontend domain
        'Access-Control-Allow-Methods': 'POST, OPTIONS',
        'Access-Control-Allow-Headers': 'Content-Type',
        'Access-Control-Max-Age': '86400',
      });
  }
  
  if (e.parameter.action === 'updateTicketStatus') {
    const requestData = JSON.parse(e.postData.contents);
    const success = updateTicketStatus(requestData);

    if (success) {
      return ContentService.createTextOutput("Ticket status updated successfully")
        .setMimeType(ContentService.MimeType.TEXT)
        .setHeaders({ 'Access-Control-Allow-Origin': '*' }); // Allow all origins, you can restrict this to your frontend domain
    } else {
      return ContentService.createTextOutput("Failed to update ticket status")
        .setMimeType(ContentService.MimeType.TEXT)
        .setHeaders({ 'Access-Control-Allow-Origin': '*' }); // Allow all origins, you can restrict this to your frontend domain
    }
  }
}