简体   繁体   中英

Date formatting from Google Spreadsheet to Telegram bot

I make a bot which is connected with spreadsheets with Webhook.

I have a cell with a date formatted like 'dd MMMM' , eg "03 september" . It is got from a certain amount of milliseconds which is equal to 03.09.2020 00:00:00 GMT+3. I want to use this exact value ( "03 september" ) as a poll option.

You can see my code below.

If I use it as it is, poll option is somehow converted into the value "2020-09-02T21:00:00.000" . If I map all the dates into Strings before sending it into JSON then this option looks like "Thu Sep 03 2020 00:00:00 GMT+0300 ... " . How can I keep it looking simple in telegram?

UPD: I figured that Utilities.formatDate() is a proper solution for this, but still I don't know how to format month name into russian locale.

function doWork() {
  var availableDates = scheduleSheet.getRange(1, 1, 1, 5).getValues()[0];
  //var stringDates = availableDates.map(function(date) {return String(date)});
  sendPoll(chatId, availableDates);
}

function sendPoll(chatId, options) {
  var data = {
    method: "post",
    payload: {
      method: "sendPoll",
      chat_id: String(chatId),
      question: "Some question:",
      options: JSON.stringify(options),
      is_anonymous: false,
      allows_multiple_answers: true,
      reply_markup: JSON.stringify(REMOVE_KEYBOARD)
    }
  };
  UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/', data);
}

Posting this for documentation purposes.

As Tanaike suggested, in order to keep the displayed formats when retrieving dates (or any value) from sheet cells in Apps Script, use getDisplayValues() instead of getValues() :

Returns a two-dimensional array of displayed values, indexed by row, then by column. The values are String objects. The displayed value takes into account date, time and currency formatting, including formats applied automatically by the spreadsheet's locale setting .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM