Привет, я новичок в скриптах Google, как мне разобрать webhook? - PullRequest
0 голосов
/ 10 апреля 2019

У меня есть некоторый код, отправляемый из моего POS, я добавил скрипт Google в свою электронную таблицу, и он получает данные веб-крюка.Хотя он разделяет отметку времени на одну ячейку, затем на пару пустых ячеек, а затем на всю эту строку в последней ячейке.Вот Снимок экрана из листов Google.Где строка ниже находится в 6-х клетках.

{ text:
   'A Payment was made\n\nFor customer Cash Drawer Register for amount: 1.0 the message\nwas:',
  html:
   'A Payment was made \n\n For customer Cash Drawer Register for amount: 1.0 the message was: ',
  link: 'https://****.****.com/payments/****',
  attributes:
   { id: 12986288,
     created_at: '2019-04-10T11:35:05.832+10:00',
     updated_at: '2019-04-10T11:35:05.832+10:00',
     success: true,
     payment_amount: 1,
     invoice_ids: [ 14066768 ],
     ref_num: '',
     applied_at: '2019-04-10',
     payment_method: 'Cash',
     transaction_response: null,
     customer:
      { id: 7960296,
        firstname: 'Cash Drawer',
        lastname: 'Register',
        fullname: 'Cash Drawer Register',
        business_name: 'Cash Drawer Register',
        email: null,
        phone: null,
        mobile: null,
        created_at: '2017-02-02T09:12:09.186+11:00',
        updated_at: '2019-04-10T11:35:05.828+10:00',
        pdf_url: null,
        address: null,
        address_2: null,
        city: null,
        state: null,
        zip: null,
        latitude: null,
        longitude: null,
        notes: null,
        get_sms: false,
        opt_out: false,
        disabled: false,
        no_email: false,
        location_name: null,
        location_id: null,
        properties: {},
        online_profile_url: null,
        tax_rate_id: null,
        notification_email: null,
        invoice_cc_emails: null,
        invoice_term_id: null,
        referred_by: null,
        ref_customer_id: null,
        business_and_full_name: 'Cash Drawer Register - Cash Drawer Register',
        business_then_name: 'Cash Drawer Register' } } }

Я просто использую этот базовый скрипт webhook ниже, поэтому мне просто нужна помощь, чтобы разобрать его, чтобы я мог получить детали в коде выше.Мне нужно: id: имя: фамилия: количество: в отдельных ячейках, как это снимок экрана

 //this is a function that fires when the webapp receives a GET request
function doGet(e) {
  return HtmlService.createHtmlOutput("request received");
}

//this is a function that fires when the webapp receives a POST request
function doPost(e) {
  var params = JSON.stringify(e.postData.contents);
  params = JSON.parse(params);
  var myData = JSON.parse(e.postData.contents);
  var testRunUrl = myData.test_run_url;
  var testRunName = myData.test_name;
  var testRunEnv = myData.environment_name;
  var testRunResult = myData.result;
  var sheet = SpreadsheetApp.getActiveSheet();
  var lastRow = Math.max(sheet.getLastRow(),1);
  sheet.insertRowAfter(lastRow);
  var timestamp = new Date();
  sheet.getRange(lastRow + 1, 1).setValue(timestamp);
  sheet.getRange(lastRow + 1, 2).setValue(testRunName);
  sheet.getRange(lastRow + 1, 3).setValue(testRunEnv);
  sheet.getRange(lastRow + 1, 4).setValue(testRunResult);
  sheet.getRange(lastRow + 1, 5).setValue(testRunUrl);
  sheet.getRange(lastRow + 1, 6).setValue(params);
  SpreadsheetApp.flush();
  return HtmlService.createHtmlOutput("post request received");
}

1 Ответ

0 голосов
/ 10 апреля 2019

Вы пробовали этот код?

function doPost(e) {
      var content = JSON.parse(e.postData.contents);

      return ContentService
              .createTextOutput()
              .setMimeType(ContentService.MimeType.JSON)
              .setContent(JSON.stringify(content));
}
...