LANG SELRCT

Apps Script Reference  (Create: Create new Spreadsheet | Create new Apps Script

Saturday, April 6, 2019

今日の年月日と曜日を取得したい


こういうふうに取得してみる





コード.gs
function doGet() {
  return HtmlService.createHtmlOutputFromFile("index");
}
意訳
この機能がやること
指定したHTMLファイルを表示する




index.html
<!DOCTYPE html>
<html>
  <body>
  <input type="text" id="today"><br>
<script>
elem('today').value = getToday();

function elem(id) {
  return document.getElementById(id);
}

function add0(times) {
  for(var i = 0; i < times.length; i++) {
    if(times[i] < 10) {
      times[i] = "0" + times[i];
    }
  }
  return times;
}

function getToday() {
  var now = new Date(); 
  var year = now.getFullYear();
  var month = now.getMonth() + 1;
  var dayNum = now.getDay();
  var day = getDay(dayNum);
  var date = now.getDate();
  var today = add0([year, month, date, day]).join('/');
  return today;
}

function getDay(dayNum) {
  var SEVEN_DAYS = "日月火水木金土";
  var day = SEVEN_DAYS[dayNum];
  return day;
}

</script>
  </body>
</html>





Latest post

Extracting data from Google Sheets with regular expressions

Introduction Regular expressions are a powerful tool that can be used to extract data from text.  In Google Sheets, regular expressions ca...