解决Excel数据分析问题

问题描述 投票:0回答:1

enter image description here

我有一张包含 3 列的 Excel 表,所有不同的药物名称都用逗号分隔,我需要输出为

enter image description here

我需要一些代码或方法来执行此操作,因为数据量非常巨大

python excel windows-subsystem-for-linux
1个回答
0
投票

这是一个 typescript 函数来完成这项工作
有点混乱,但评论应该使它具有一定的可读性

只需将其复制粘贴到自动化选项卡下

function removeSpaces(text: string){
  while (text[0] == ' ') {
    text = text.substring(1, text.length);
  }
  return text;
}

function main(workbook: ExcelScript.Workbook) {
  // Get the used range of the current worksheet.
  const sheet = workbook.getActiveWorksheet();
  const range = sheet.getUsedRange();
  
  // create new worksheet
  const name = "fixed"
  let newSheet = workbook.getWorksheet(name)
  if (newSheet){
    newSheet.delete();
  }
  newSheet = workbook.addWorksheet(name)
  newSheet.setTabColor("purple");
  newSheet.activate();

  const newTable = newSheet.addTable("A1:C1", true);
  newTable.setName(name);



  // cycle through selection
  let rowi = 0;
  const sy = range.getRowCount();
  const selValues = range.getValues(); // array of array of values
  for (let i=0; i<sy; i++){
    // get row values
    const row = selValues[i];

    // get third value of row
    let v = row[2].toString();
    v = v.replace(/"/g, ''); // remove quotes

    // split by comma
    let elems = v.split(',')
    elems.map((elem) => {
      elem = removeSpaces(elem); // clean up spaces
      newTable.addRow(rowi, [row[0], row[1], elem]); // add new row
      rowi++;
    });
  }
}

之前:

之后:

© www.soinside.com 2019 - 2024. All rights reserved.