Skip to content

Instantly share code, notes, and snippets.

@sftblw
Last active December 4, 2015 06:43
Show Gist options
  • Save sftblw/9e7b862c4b1ebb8b749a to your computer and use it in GitHub Desktop.
Save sftblw/9e7b862c4b1ebb8b749a to your computer and use it in GitHub Desktop.
note generation from monday to friday (node js)

What it is

From specified date (or today), make 5 .docx files, from monday ~ friday, from specified template.

Date on filename will be zero-filled.

korean: 연구노트 만들기가 귀찮아서, 적당한 .docx 템플릿 패키지가 보이길래 날짜 계산해서 월요일부터 금요일까지 그 주의 연구노트 파일 5개를 만드는 간단한 소스를 작성하였습니다. 특정 날짜가 주어지면 그 날짜가 포함되어있거나 그 이전의 5일 (월~금) 을 찾아서 만듭니다.

파일 이름의 숫자가 제로패딩되는 건 덤. (제로패딩은 stackoverflow 에서 주섬주섬 주워왔습니다...)

Example

input : today or specified (2015.11.03 as today →)
output : Five files (monday ~ friday)

FileName_20151102_월.docx (monday)
FileName_20151103_화.docx (tuesday)
...
FileName_20151106_금.docx (friday)

How to

  1. npm install
  2. put template file input.docx, and edit your template.
  • template fields : {year} {month} {date} {day} {subject}
  1. node ./notegen.js
  • example : node ./notegen.js -d 2015.11.09 -s "멋진 연구" -n "아름다운_파일_"
  • All options can be omitted, for default, edit notegen.js
  • -d for date, YYYY.MM.DD format
  • -s for {subject} field
  • -n for file name postfix
'use strict';
var fs = require('fs');
var Docxtemplater = require('docxtemplater');
var day_format = ['일', '월', '화', '수', '목', '금', '토'];
var templateData = {
year: '2015',
month: '5',
date: '9',
day: '월',
subject: '-s 인수에 주제를 넣으세요',
name: '-n 인수에 파일이름을 넣으세요'
};
var monday = new Date();
for (let i = 0; i < process.argv.length; i++) {
var arg = process.argv;
if ((arg[i] === '-d') && (i < process.argv.length - 1)) {
var dateStr = arg[i + 1];
if (!dateStr.trim().match(/^\d{4}\.\d{2}.\d{2}$/)) {
console.log('Error : -d 날짜는 이 형식에 맞추세요. yyyy.mm.dd');
console.log('예제 : 1992.05.09');
}
var matched = dateStr.trim().match(/^(\d{4})\.(\d{2}).(\d{2})$/);
templateData.year = matched[1];
templateData.month = matched[2];
templateData.date = matched[3];
monday = new Date(Number.parseInt(matched[1]), Number.parseInt(matched[2]) - 1, Number.parseInt(matched[3]));
monday.setFullYear(Number.parseInt(matched[1]));
i++;
}
if ((arg[i] === '-s') && (i < process.argv.length - 1)) {
templateData.subject = arg[i + 1];
i++;
}
if ((arg[i] === '-n') && (i < process.argv.length - 1)) {
templateData.name = arg[i + 1];
i++;
}
if ((arg[i] === 'help') && (i < process.argv.length - 1)) {
console.log('command : -d -s');
console.log('-d yyyy.mm.dd');
console.log('-s "some text"');
}
}
// set as monday
monday.setDate(monday.getDate() - (monday.getDay() - 1));
if (monday > new Date()) {
monday.setDate(monday.getDate() - 7);
}
for (let i = 0; i < 5; i++) {
var today = new Date(monday);
today.setDate(monday.getDate() + i);
templateData.year = today.getFullYear();
templateData.month = today.getMonth() + 1;
templateData.date = today.getDate();
templateData.day = day_format[today.getDay()];
var content = fs.readFileSync(__dirname + '/연구노트_input.docx', 'binary');
var doc = new Docxtemplater(content);
doc.setData(templateData);
doc.render();
var buf = doc.getZip().generate({type: 'nodebuffer'});
fs.writeFileSync(__dirname + '/' + templateData.name +
templateData.year.toString() + pad(templateData.month, 2, 0).toString() + pad(templateData.date, 2, 0).toString() +
'_' + day_format[today.getDay()] +
'.docx', buf);
}
// from somewhere of stackoverflow
function pad (n, width, z) {
z = z || '0';
n = n + '';
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
}
{
"name": "notegen",
"version": "0.0.1",
"description": "",
"main": "notegen.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "sftblw",
"license": "MIT",
"dependencies" : {
"docxtemplater": "^1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment