简体   繁体   English

从 Google 电子表格到 Telegram bot 的日期格式

[英]Date formatting from Google Spreadsheet to Telegram bot

I make a bot which is connected with spreadsheets with Webhook.我使用 Webhook 制作了一个与电子表格连接的机器人。

I have a cell with a date formatted like 'dd MMMM' , eg "03 september" .我有一个日期格式为'dd MMMM'的单元格,例如"03 september" It is got from a certain amount of milliseconds which is equal to 03.09.2020 00:00:00 GMT+3.它是从等于 03.09.2020 00:00:00 GMT+3 的特定毫秒数获得的。 I want to use this exact value ( "03 september" ) as a poll option.我想使用这个确切的值( "03 september" )作为投票选项。

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" .如果我按原样使用它, poll 选项会以某种方式转换为值"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 ... " .如果我在将所有日期发送到 JSON 之前将其映射到字符串中,那么此选项看起来像"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. UPD:我认为Utilities.formatDate()是一个合适的解决方案,但我仍然不知道如何将月份名称格式化为俄语语言环境。

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() :正如Tanaike 所建议的,为了在从 Apps 脚本中的工作表单元格检索日期(或任何值)时保持显示的格式,请使用getDisplayValues()而不是getValues()

Returns a two-dimensional array of displayed values, indexed by row, then by column.返回显示值的二维数组,按行索引,然后按列索引。 The values are String objects.值是 String 对象。 The displayed value takes into account date, time and currency formatting, including formats applied automatically by the spreadsheet's locale setting .显示的值考虑了日期、时间和货币格式,包括电子表格的区域设置自动应用的格式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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