需要有关创建 Apps 脚本或表格公式以将子 sheet 名称与唯一 ID 列表相匹配的指导
Need guidance on creating an Apps script or Sheets formula to match sub-sheet names to a list of unique IDs
这里有更详细的信息。我有一个包含 16 个工作簿的列表,其中有一个按姓氏组织的子 sheet 列表,每个工作簿的编号从 15-100 个名字不等。每个人都有一个唯一的身份证号码,需要放在各自的子sheet中。我有一个单独的文件,其中包含需要插入的名称和 ID。我需要创建一个脚本或一个 sheets 公式:
遍历所有子sheets,获取sheet名称,在另一个文件中找到名称和ID,将ID插入正确子[=中的单元格38=],然后转到工作簿中的下一个 sheet。我对表格和脚本非常陌生,所以我很迷茫。还有一个复杂的问题:
- 子sheet的组织不统一,格式混合,大多数只是姓氏,但如果存在重复的姓氏,则使用各种约定(例如:Smith,F_Smith , John Smith, 都在工作簿中用来指代 3 个不同的人)
因为我有一个包含我需要的所有 ID 和人员的列表,所以这没什么大不了的,因为脚本可以根据姓氏进行匹配,而且我可以手动更改重复项。
Here is an anonymized sheet with sample names and IDs
Here is an example sheet with last names as sub-sheets
感谢任何帮助,谢谢。
已更新 table 名称和 ID sheet:
First Name
Last Name
ID
Dave
Smith
247
Jack
Smith
248
Jane
Doe
143
Evelyn
Borca
1292
Cherie
Tenny
1148
Brent
Brooks
285
带有需要根据姓氏匹配插入 ID 的子 sheet 的工作簿屏幕截图 - https://i.ibb.co/Ydpqvyn/workbook-example.jpg
function loademplidsintheresheets() {
const niss = SpreadsheetApp.openById('nissid');
const sh = niss.getSheetByName('Names and Ids');//spreadsheet with names and id
const vs = sh.getDataRange().getDisplayValues();
let ids = { pA: [] };//ids object
let cnt = { pA: [] };//names count object
vs.forEach(r => {
if(!cnt.hasOwnProperty(r[1])) {
cnt[r[1]]=1;
cnt.pA.push(r[1]);
} else {
cnt[r[1]] += 1;
}
});
vs.forEach(r => {
if(!ids.hasOwnProperty(r[1])) {
if(cnt[r[1]] > 1) {
ids[`${r[0].charAt(0)}_${r[1]}`] = r[2];//if cnt > 1
} else {
ids[r[1]] = r[2];//in last name is unique
}
}
});
const ss = SpreadsheetApp.getActive();//assume spreadsheet ids are in the active spreadsheet
const wsh = ss.getSheetByName('Spreadsheeds and Ids');
const [hA,...wbs] = wsh.getDataRange().getDisplayValues();
const idx ={};
hA.forEach((h,i) => idx[h]=i);
wbs.forEach(r =>{
let twb = SpreadsheetApp.openById(r[idx['id header']]);
twb.getSheets().forEach(s => {
s.getRange('A1Notation for the cell you want the id in').setValue(ids[sh.getName()]);
});
})
}
这里有更详细的信息。我有一个包含 16 个工作簿的列表,其中有一个按姓氏组织的子 sheet 列表,每个工作簿的编号从 15-100 个名字不等。每个人都有一个唯一的身份证号码,需要放在各自的子sheet中。我有一个单独的文件,其中包含需要插入的名称和 ID。我需要创建一个脚本或一个 sheets 公式:
遍历所有子sheets,获取sheet名称,在另一个文件中找到名称和ID,将ID插入正确子[=中的单元格38=],然后转到工作簿中的下一个 sheet。我对表格和脚本非常陌生,所以我很迷茫。还有一个复杂的问题:
- 子sheet的组织不统一,格式混合,大多数只是姓氏,但如果存在重复的姓氏,则使用各种约定(例如:Smith,F_Smith , John Smith, 都在工作簿中用来指代 3 个不同的人)
因为我有一个包含我需要的所有 ID 和人员的列表,所以这没什么大不了的,因为脚本可以根据姓氏进行匹配,而且我可以手动更改重复项。
Here is an anonymized sheet with sample names and IDs
Here is an example sheet with last names as sub-sheets
感谢任何帮助,谢谢。
已更新 table 名称和 ID sheet:
First Name | Last Name | ID |
---|---|---|
Dave | Smith | 247 |
Jack | Smith | 248 |
Jane | Doe | 143 |
Evelyn | Borca | 1292 |
Cherie | Tenny | 1148 |
Brent | Brooks | 285 |
带有需要根据姓氏匹配插入 ID 的子 sheet 的工作簿屏幕截图 - https://i.ibb.co/Ydpqvyn/workbook-example.jpg
function loademplidsintheresheets() {
const niss = SpreadsheetApp.openById('nissid');
const sh = niss.getSheetByName('Names and Ids');//spreadsheet with names and id
const vs = sh.getDataRange().getDisplayValues();
let ids = { pA: [] };//ids object
let cnt = { pA: [] };//names count object
vs.forEach(r => {
if(!cnt.hasOwnProperty(r[1])) {
cnt[r[1]]=1;
cnt.pA.push(r[1]);
} else {
cnt[r[1]] += 1;
}
});
vs.forEach(r => {
if(!ids.hasOwnProperty(r[1])) {
if(cnt[r[1]] > 1) {
ids[`${r[0].charAt(0)}_${r[1]}`] = r[2];//if cnt > 1
} else {
ids[r[1]] = r[2];//in last name is unique
}
}
});
const ss = SpreadsheetApp.getActive();//assume spreadsheet ids are in the active spreadsheet
const wsh = ss.getSheetByName('Spreadsheeds and Ids');
const [hA,...wbs] = wsh.getDataRange().getDisplayValues();
const idx ={};
hA.forEach((h,i) => idx[h]=i);
wbs.forEach(r =>{
let twb = SpreadsheetApp.openById(r[idx['id header']]);
twb.getSheets().forEach(s => {
s.getRange('A1Notation for the cell you want the id in').setValue(ids[sh.getName()]);
});
})
}