首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将行数据移动到某些选项卡

将行数据移动到某些选项卡
EN

Stack Overflow用户
提问于 2022-05-30 15:28:15
回答 1查看 146关注 0票数 0

我收到一个每日Excel文件,其中有大约15K行和22列。A列中有几百个不同的值。我想要复制或移动某些行到预定义的选项卡中。例如,值"Home“的所有内容都将被移动到Sheet2,"Work”将被移动到Sheet3等。

有什么指南可以为这个或想法遵循?

EN

回答 1

Stack Overflow用户

发布于 2022-06-01 00:06:33

这是我尝试的一个脚本,得到了我想要的结果。注意'getUsedRange()‘方法的用法。它帮助您访问各种大小的范围。在类型记录/javascript中,或者通过使用Excel进行过滤,可能还有其他几种方法可以做到这一点。

代码语言:javascript
复制
function main(workbook: ExcelScript.Workbook) {
  //get the used range, assuming there is one worksheet
  let worksheet = workbook.getActiveWorksheet();
  let range = worksheet.getUsedRange();
  let rangeValues = range.getValues();
  
  // create an array to hold all the unique values in the first column skipping the header row
  let firstCol: string[] = [];
  rangeValues.forEach((curRow, index) => {
    if (index > 0) {
      firstCol.push(curRow[0].toString());
    }
  });
  let uniqueFirstCol = firstCol.filter((val, ind, arr) => arr.indexOf(val) === ind);

  // run thru all the unique values, filter as appropriate and add filtered values to new worksheets
  uniqueFirstCol.forEach(v => {
    let tempValues = rangeValues.filter((row, index) => row[0] === v);
    let newSheet = workbook.addWorksheet(v);
    let newRange = newSheet.getRangeByIndexes(1,0, tempValues.length, tempValues[0].length);
    newRange.setValues(tempValues);
    newSheet.getRangeByIndexes(0,0, 1, rangeValues[0].length).setValues([rangeValues[0]])
  });
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72436709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档