【问题标题】:How to add data validation list in excel using javascript如何使用javascript在excel中添加数据验证列表
【发布时间】:2020-02-10 11:41:57
【问题描述】:

> 我正在创建和下载 excel 文件,其中包含我以 JSON 格式获得的数据。现在我想在最后添加状态列并在那里提供列表数据验证,其中包含三个指定值“拒绝”、“选择”和“保留”

 downloadTableData(data, headers) {
  this.dataToDownload = "";
  let dataSourceLength = data.length;
  let rowData = '';
  for (let i = 0; i < dataSourceLength; i++) {
    let line = '';
    for (let key in data[i]) {
      if (line != '') {
        line = line + ','
      }
      line = line + data[i][key];
    }
    rowData = rowData + line + "\r\n";
  }

  // as of now; but based on api data, row data and column dat ashould be done
  this.dataToDownload = this.dataToDownload + headers.join(',') + "\r\n" + rowData;

  if (this.dataToDownload.split('\n').length - 1 >= 1) {
    // const fileName = 'reports-' + new Date();
    const fileName ='Upload_template.xlsx';
    let anchor = document.createElement('a');
    anchor.href = URL.createObjectURL(new Blob([this.dataToDownload], { type: 'text/csv' }));
    anchor.download = fileName + '.csv';
    // anchor.download = fileName;
    // start download
    anchor.click();
  }

}

【问题讨论】:

    标签: javascript excel csv office-js


    【解决方案1】:

    我在 excel.js 和 file-saver.js 的帮助下找到了解决方案。

    import { Workbook } from 'exceljs';
    import * as fs from 'file-saver';
    generateExcel(list,header) {
      let data:any = [];
      for(let i=0;i<list.length;i++){
        let arr = [list[i].requisitionId,list[i].applicationid, list[i].candidateid, list[i].unitcode];
        data.push(arr);
      }
      console.log(data);
      //Create workbook and worksheet
      let workbook = new Workbook();
      let worksheet = workbook.addWorksheet('Candidate Report');
    
      //Add Header Row
      let headerRow = worksheet.addRow(header);
    
      // Cell Style : Fill and Border
      headerRow.eachCell((cell, number) => {
        cell.fill = {
          type: 'pattern',
          pattern: 'solid',
          fgColor: { argb: 'FFFFFF00' },
          bgColor: { argb: 'FF0000FF' }
        }
        cell.border = { top: { style: 'thin' }, left: { style: 'thin' }, bottom: { style: 'thin' }, right: { style: 'thin' } }
      })
      worksheet.getColumn(3).width = 30;
      data.forEach(d => {
        let row = worksheet.addRow(d);
      }
      );
      list.forEach((element,index) =>{
        worksheet.getCell('E'+(+index+2)).dataValidation = {
          type: 'list',
          allowBlank: true,
          formulae: ['"Selected,Rejected,On-hold"']
      };
      })
      //Generate Excel File with given name
      workbook.xlsx.writeBuffer().then((data) => {
        let blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
        fs.saveAs(blob, 'candidate.xlsx');
      })
    
    }
    

    `

    【讨论】:

      【解决方案2】:

      这里是如何应用数据验证的示例代码

      const nameSourceRange = context.workbook.worksheets.getItem("Status").getRange("A1:A3");
      
      let approvedListRule = {
        list: {
          inCellDropDown: true,
          source: nameSourceRange
        }
      };
      nameRange.dataValidation.rule = approvedListRule;
      

      我为您创建了一个要点来演示如何添加已批准状态。 https://gist.github.com/lumine2008/827ab26a65b76a5826331d960323c43b

      【讨论】:

      猜你喜欢
      • 2022-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-07
      • 2014-03-05
      • 1970-01-01
      相关资源
      最近更新 更多