Google 表格脚本 - 如何根据另一个 sheet 中的某些字段复制 sheet
Google Sheets Script - How to duplicate a sheet based on certain fields in another sheet
这里是发布问题的新手,也是 Google 表格使用 Apps 脚本的新手。
我正在尝试让宏根据以“~”开头的模板 sheet 创建一个新的 sheet。宏应该在另一个 sheet 的 A 列中找到以“Y”开头的任何内容。如果它有一个“Y”,它应该在同一行但 B 列上获取供应商名称值。然后这将用于创建新作品sheet 并为其命名。
下面的代码执行了,但是没有任何反应。我能够复制模板 sheet,但在供应商 sheet 选项卡中添加搜索似乎导致了问题。任何帮助表示赞赏。我也提前道歉,第一次发布所以不太确定这一切是否有意义。
function DuplicateSht() {
var Sect = 0
var Vend = 0
var sh = SpreadsheetApp.getActiveSpreadsheet()
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var data = sh.getDataRange().getValues(); //
for (var i=0 ; i<sheets.length ; i++) {
SpreadsheetApp.setActiveSheet(sheets[i])
if (sheets[i].getName().startsWith("Vendor Sheet")){//locates sheet with base information
Sect = sh.getRange("C2").getValue();//pulls in section number
Vend = sh.getRange("C3").getValue();//pulls in number of vendors
for (var j=10 ; j<Vend+10; j++){
if(data[j][1] == "Y"){//Looks for each vendor with a "Y" in column A. The number of vendors to review is variable
var VendName = sh.getRange("B" + j).getValue();//If a vendor has a "Y" will grab their name
for (var k=0 ; k<sheets.length ; k++) {
SpreadsheetApp.setActiveSheet(sheets[k])
for (var l=0 ; l<Vend ; l++){
if (sheets[l].getName().startsWith("~")){
sh.duplicateActiveSheet()//duplicates the sheet beginning with "~"
sh.renameActiveSheet("x-" + VendName + " " + Sect)//renames the sheet based on the data from the prep template sheet, including vendor name
}
}
}
}
}
}
}
}
我对您面临的挑战的理解是,您有一个包含各种供应商信息的传播sheet。供应商都列在一个 sheet 上。然后,当每个供应商在该列表的其中一列中获得“Y”时 sheet,将获得一个基于模板创建的专用 sheet。
如果此结构正确,则您可以避免原始代码中的嵌套语句,更直接地寻找供应商和创建 sheets。
这是我创建的 the spreadsheet,它演示了这一点并显示了以下代码的运行情况。
function DuplicateSht() {
//since there is only one Vendor Sheet we can just set a variable pointing to it, then get the data in the sheet using getDataRange
var ss = SpreadsheetApp.getActiveSpreadsheet()
var ws = ss.getSheetByName("Vendor Sheet")
var data = ws.getDataRange().getValues()
//this "reduce function" filters the data so we have an array ("vendors") that only includes those rows with a "Y" in the first column
var vendors = data.reduce(function(acc, curr) {
if(curr[0] === "Y") {
acc.push(curr)
}
return acc
},[])
//now loop through the vendors list and for each one set up the desired sheet name -- before duplicating the template, make sure it doesn't already exist.
for (i=0; i<vendors.length; i++){
var sect = vendors[i][2]
var name = "x-" + vendors[i][1] + " " + sect
//get the names of all the sheets
var sheets = ss.getSheets().map(function (r){return r.getName()})
//check to see if the current vendor you are evaluating has already had a sheet created for it. If not, then create a new sheet based on the template
if (sheets.indexOf(name) === -1){
ss.setActiveSheet(ss.getSheetByName("~Template"))
ss.duplicateActiveSheet()
ss.renameActiveSheet(name)
}
}
}
这里是发布问题的新手,也是 Google 表格使用 Apps 脚本的新手。
我正在尝试让宏根据以“~”开头的模板 sheet 创建一个新的 sheet。宏应该在另一个 sheet 的 A 列中找到以“Y”开头的任何内容。如果它有一个“Y”,它应该在同一行但 B 列上获取供应商名称值。然后这将用于创建新作品sheet 并为其命名。
下面的代码执行了,但是没有任何反应。我能够复制模板 sheet,但在供应商 sheet 选项卡中添加搜索似乎导致了问题。任何帮助表示赞赏。我也提前道歉,第一次发布所以不太确定这一切是否有意义。
function DuplicateSht() {
var Sect = 0
var Vend = 0
var sh = SpreadsheetApp.getActiveSpreadsheet()
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var data = sh.getDataRange().getValues(); //
for (var i=0 ; i<sheets.length ; i++) {
SpreadsheetApp.setActiveSheet(sheets[i])
if (sheets[i].getName().startsWith("Vendor Sheet")){//locates sheet with base information
Sect = sh.getRange("C2").getValue();//pulls in section number
Vend = sh.getRange("C3").getValue();//pulls in number of vendors
for (var j=10 ; j<Vend+10; j++){
if(data[j][1] == "Y"){//Looks for each vendor with a "Y" in column A. The number of vendors to review is variable
var VendName = sh.getRange("B" + j).getValue();//If a vendor has a "Y" will grab their name
for (var k=0 ; k<sheets.length ; k++) {
SpreadsheetApp.setActiveSheet(sheets[k])
for (var l=0 ; l<Vend ; l++){
if (sheets[l].getName().startsWith("~")){
sh.duplicateActiveSheet()//duplicates the sheet beginning with "~"
sh.renameActiveSheet("x-" + VendName + " " + Sect)//renames the sheet based on the data from the prep template sheet, including vendor name
}
}
}
}
}
}
}
}
我对您面临的挑战的理解是,您有一个包含各种供应商信息的传播sheet。供应商都列在一个 sheet 上。然后,当每个供应商在该列表的其中一列中获得“Y”时 sheet,将获得一个基于模板创建的专用 sheet。
如果此结构正确,则您可以避免原始代码中的嵌套语句,更直接地寻找供应商和创建 sheets。
这是我创建的 the spreadsheet,它演示了这一点并显示了以下代码的运行情况。
function DuplicateSht() {
//since there is only one Vendor Sheet we can just set a variable pointing to it, then get the data in the sheet using getDataRange
var ss = SpreadsheetApp.getActiveSpreadsheet()
var ws = ss.getSheetByName("Vendor Sheet")
var data = ws.getDataRange().getValues()
//this "reduce function" filters the data so we have an array ("vendors") that only includes those rows with a "Y" in the first column
var vendors = data.reduce(function(acc, curr) {
if(curr[0] === "Y") {
acc.push(curr)
}
return acc
},[])
//now loop through the vendors list and for each one set up the desired sheet name -- before duplicating the template, make sure it doesn't already exist.
for (i=0; i<vendors.length; i++){
var sect = vendors[i][2]
var name = "x-" + vendors[i][1] + " " + sect
//get the names of all the sheets
var sheets = ss.getSheets().map(function (r){return r.getName()})
//check to see if the current vendor you are evaluating has already had a sheet created for it. If not, then create a new sheet based on the template
if (sheets.indexOf(name) === -1){
ss.setActiveSheet(ss.getSheetByName("~Template"))
ss.duplicateActiveSheet()
ss.renameActiveSheet(name)
}
}
}