简体   繁体   English

如何使用地图将一列分隔到他们自己的工作表中?

[英]How can I separate a column into their own sheet using maps?

I am trying to use this map provided by a previous question I had.我正在尝试使用我之前的问题提供的这个 map。

In this case, it takes the sales rep's name, creates a sheet with their name and then moves all orders that belong to them to their sheet.在这种情况下,它会获取销售代表的姓名,使用他们的姓名创建一个工作表,然后将属于他们的所有订单移动到他们的工作表中。 This is what the code looks like.这就是代码的样子。

function sortByRep(sheet){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("All Orders");
  var range = sheet.getRange("A2:H" + sheet.getLastRow());
  var values = range.getValues();
  var sheets = [...new Set(values.map(r => r[6]))].reduce((o, e) => (o[e] = ss.getSheetByName(e) || ss.insertSheet(e), o), {});
  [...values.reduce((m, r) => m.set(r[6], m.has(r[6]) ? [m.get(r[6]), r] : r), new Map())]
    .forEach(([k, v]) => {
      var s = sheets[k];
      if (s) s.getRange(s.getLastRow() + 1, 1, v.length, v[0].length).setValues(v);
    });

But I get this error:但我得到这个错误:

Exception: The parameters (number,number,number,null) don't match the method signature for SpreadsheetApp.Sheet.getRange.例外:参数 (number,number,number,null) 与 SpreadsheetApp.Sheet.getRange 的方法签名不匹配。

I think it has to be because there are spaces in the names?我认为这必须是因为名称中有空格? But I'm not sure.但我不确定。 在此处输入图像描述

I have added this picture as an example.我添加了这张图片作为示例。 I want my script to make a new sheet with the sales rep name and move all of their orders to that sheet.我希望我的脚本使用销售代表的姓名制作一张新工作表,并将他们的所有订单移至该工作表。

In your script, how about the following modification?在您的脚本中,如何进行以下修改?

From:从:

[...values.reduce((m, r) => m.set(r[6], m.has(r[6]) ? [m.get(r[6]), r] : r), new Map())]
  .forEach(([k, v]) => {
    var s = sheets[k];
    if (s) s.getRange(s.getLastRow() + 1, 1, v.length, v[0].length).setValues(v);
  });

To:至:

[...values.reduce((m, r) => m.set(r[6], m.has(r[6]) ? [...m.get(r[6]), r] : [r]), new Map())]
  .forEach(([k, v]) => {
    var s = sheets[k];
    if (s) s.getRange(s.getLastRow() + 1, 1, v.length, v[0].length).setValues(v);
  });
  • In this modification, both one row and multiple rows can be used.在此修改中,可以使用一排和多排。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将某些对象键分成自己的数组? - How can I separate certain object keys into their own array? 如何在同一个excel文件中将数据分隔到新工作表 - How can I separate data to new sheet in same excel file React/MaterialUI - 如何在我的 object 中将每个 JSON 数组 map 放到它自己的单独表中? - React/MaterialUI - How can I map each JSON array in my object to it's own separate table? 如何使用Javascript使用Sprite Sheet创建动画按钮? - How can I create an Animated Button with a Sprite Sheet using Javascript? 如何使用 getJSON(或其他方式)解析 Google 表格? - How can I parse a Google sheet using getJSON (or other way)? 如何使用 Google 脚本从 1 个 Google 工作表中复制特定值并粘贴到另一个工作表? - How can I copy specific values from 1 Google Sheet and paste to another Sheet using Google Script? 我无法使用谷歌应用脚本隐藏谷歌表格的最后一列 - I can't hide the last column of a google sheet using google apps scripts 如何使用JQuery引用单独的html文件的ID? - How can I reference the ID of a separate html file using JQuery? 我如何使用javascript将数字乘以9的1到1000的数字? - how can i separate the numbers multiple by 9 in 1 to 1000 numbers using javascript? 如何使用样式组件分隔这两个箭头? - How can I separate this two arrows using styled-components?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM