0
0

More than 1 year has passed since last update.

再帰的にフォルダパスを取得する(node)

Posted at

lib xlsx install

xlsx(SheetJS)()

npm install xlsx

source

let XLSX = require('xlsx')
let workbook = XLSX.readFile('./xls/ex_-01.xlsx')
let input_sheet = workbook.Sheets['input']
let input_json = XLSX.utils.sheet_to_json(input_sheet)
// console.log( input_json )
let ab_path = [];

function getPath(json, folder_id) {
  var tmp1 = json.find((v) => v.FOLDER_ID == folder_id);
  if (!tmp1['PARENT_FOLDER_ID']) {
    console.log(`###### PARENT_FOLDER_ID 空 : FOLDER_ID = ${folder_id} ##############`)
    return;
  }
  parent_id = tmp1['PARENT_FOLDER_ID'];
  var ret = "";
  if (parent_id != -1) {
    //console.log(tmp1['FOLDER_NAME']);
    ab_path.push(tmp1['FOLDER_NAME']);
    return getPath(json, parent_id)
  }
  ab_path.push(tmp1['FOLDER_NAME']);
  ret += '/' + ab_path.reverse().join("/")
  //console.log(ret);
  ab_path = []
  return ret
}
let output_data = [
  ['FOLDER_ID', 'AB_PATH']
];
for (let cl of input_json) {
  var tmp = "###### FOLDER_ID 空 ##############";
  //console.log( `################# ${cl['FOLDER_ID']} #################` )
  if (cl['FOLDER_ID']) {
    tmp = getPath(input_json, cl['FOLDER_ID'])
    console.log(`${cl['FOLDER_ID']}\t` + tmp)
  }
  output_data.push([cl['FOLDER_ID'], tmp]);
}

var output_sheet = "output";
var ws = XLSX.utils.aoa_to_sheet(output_data);
// TODO: remove exist sheet 
if (workbook.Sheets[output_sheet]) {
  //   console.log("remove sheet = " + output_sheet);
  console.log("update sheet = " + output_sheet);
  workbook.Sheets[output_sheet] = ws;
} else {
  /* Add the worksheet to the workbook */
  console.log("add sheet = " + output_sheet);
  XLSX.utils.book_append_sheet(workbook, ws, output_sheet);
}
XLSX.writeFile(workbook, './xls/ex_-01.xlsx', {
  type: 'xlsx'
});

// 「yyyymmdd」形式の日付文字列に変換する関数
function formatDateInYyyymmdd(date) {

  const y = date.getFullYear();
  const m = date.getMonth() + 1;
  const d = date.getDate();

  const yyyy = y.toString();
  const mm = ("00" + m).slice(-2);
  const dd = ("00" + d).slice(-2);

  return yyyy + mm + dd;
}

// 「hhmmss」形式の時刻文字列に変換する関数
function formatDateInHhmmss() {

  const h = date.getHours();
  const m = date.getMinutes();
  const s = date.getSeconds();

  const hh = ("00" + h).slice(-2);
  const mm = ("00" + m).slice(-2);
  const ss = ("00" + s).slice(-2);

  return hh + mm + ss;
}

// 使用例
const date = new Date();
const yyyymmdd = formatDateInYyyymmdd(date);
const hhmmss = formatDateInHhmmss(date);
console.log(yyyymmdd);
console.log(hhmmss);
0
0
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
0
0