[英]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?在您的脚本中,如何进行以下修改?
[...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);
});
[...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);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.