LoginSignup
13
12

More than 5 years have passed since last update.

JScriptでUnderscore.js使ってExcelとOutlook連携なツールを作る

Last updated at Posted at 2014-04-17

自分の仕事柄、いろいろな人とアポ調整して、日程確定した段階で、その旨のメールを念のため送る作業が毎日発生します。

こういうのを手作業ですると、誤送信の可能性が高くなるし、数通とはいえ意外と面倒なので、Excel & Outlook 連携のツールを作りました。

会社のPCがWindowsマシンで、処理系を自由に入れられないという人(=>自分^^;)もいると思うのでそういう人の役に立てばと思ってます。

事前準備

  • Cドライブ直下にlistというフォルダを作成して
  • Underscore.jsをダウンロードしてlistフォルダにlibというフォルダを作成して、そのlibフォルダ内に配置します。
  • メールのテンプレートとなるファイルとしてbaseTemplate.txtを作成してlist直下に置く

Outlookを操作するoutlook.jsを作成する

以下のようなコードを書きます。

var Outlook = function(){
  this.version = "1.0";
  this.Apps = WScript.CreateObject("Outlook.Application");
  this.basePath = "C:\\list\\";
};

Outlook.prototype.readTemplate = function(fileName){
  var fso = new ActiveXObject("Scripting.FileSystemObject");
  var ForReading = 1;
  if(typeof fileName === "undefined"){
    var file = this.basePath + "template.txt";
  }else{
    var file = this.basePath +fileName;
  }
  WScript.Echo( file);
  var template = fso.OpenTextFile(file,ForReading,false );
  var contents = template.readAll();
  template.close();
  return contents;

};

Outlook.prototype.excelOperate = function (){
  //  Excelオブジェクトを取得(Excelの起動)
  var ExcelApp = new ActiveXObject( "Excel.Application" );
  //  Excelアプリケーションを表示
  ExcelApp.Visible = false;

  var book = ExcelApp.Workbooks.Open( "C:\\list\\sendlist.xls" );
  var sheet = book.Worksheets( 1 );
  // Excel内をパース
  var result = [];
  var r = 0;
  while(1) {
    if (sheet.Cells(2+ r, 1).value == null)
      break;
    var ary = [];
    var c = 0;
    var sendToName = sheet.Cells(2+ r, 1).value;
    var mailAddress = sheet.Cells(2+ r, 2).value;
    var mailSubject = sheet.Cells(2+ r, 3).value;
    var reservedDate = convertDateFormat(new Date(sheet.Cells(2+ r, 7).value));
    result.push(
    {
      "sendToName":sendToName,
      "mailAddress":mailAddress,
      "mailSubject":mailSubject,
      "reservedDate":reservedDate

    });
    r++;
  }


  WScript.Sleep( 1000 );
  //  Excelを終了
  ExcelApp.Quit();
  WScript.Echo( "終了" );

  //  オブジェクトを解放
  ExcelApp = null;
  return result;
};



function convertDateFormat(dateObj){
  var month = dateObj.getMonth() + 1 + "";
  var date = dateObj.getDate() + "";
  var dayList = [ "",  "",  "",  "",  "",  "", ""];
  var day = "" + dayList[dateObj.getDay()] + "";


  if (dateObj.getHours() >= 12){
    var _temp = dateObj.getHours() - 12;
    var hour = "午後" + _temp.toString()  + "";
  }else{
    var hour = "午前" + dateObj.getHours() + "";
  }


  if( dateObj.getMinutes() === 0){
    var miniutes = "00分";
  }else{
    var miniutes = dateObj.getMinutes() + "";
  }
  return [ month,date, day,hour ,miniutes ].join("");

}

WSFファイルを準備する

こんな感じのWSFファイルを作成します。

<job>
<script language="javascript" src="lib/underscore.js"/> 
<script language="javascript" src="outlook.js"/>    
<script language="javascript">
var outlook = new Outlook();
var data = outlook.excelOperate();
var template = outlook.readTemplate("baseTemplate.txt");
var compiled = _.template(template);

_.each(data,function(d){
  var objMAIL = outlook.Apps.CreateItem(0);
  objMAIL.To = d.mailAddress;
  objMAIL.Subject = d.mailSubject;
  objMAIL.Cc = 'xxxx@xxxx.xxxx.xxx';
  objMAIL.Body = compiled(d);   
  objMAIL.display();

});

</script>
</job>

baseTemplate.txt

<%= sendToName %>様

お世話になります。

何らかのメッセージ

 <%= reservedDate %>

以上です。

13
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
12