[英]How can multiple tabs be created from a range using Google Apps Script in a Google Sheet?
I need to create a custom function to create multiple tabs in a Google Sheet based on the values in column A. How do I do this?我需要创建一个自定义 function 以根据 A 列中的值在 Google 表格中创建多个选项卡。我该怎么做?
This is for tracking the data on particular e-commerce products.这是为了跟踪特定电子商务产品的数据。 The name of them are in the first column of the first tab.
它们的名称位于第一个选项卡的第一列中。 Each entry in column A needs to have a tab labeled as the same name.
A 列中的每个条目都需要有一个标记为相同名称的选项卡。
I don't know how to do this.我不知道该怎么做。
The output should be new tabs created labeled by each cell in column A. output 应该是由 A 列中的每个单元格标记的新选项卡。
Copy and paste this code in the script editor.将此代码复制并粘贴到脚本编辑器中。
function createSheet(sheetIndex, value){
// Check if cell is empty after editing
if (value.oldValue){
value = 'Sheet' + (sheets.length - sheetIndex);
}
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
// If sheet already exits just change the sheet name
if (sheets[sheetIndex]) {
sheets[sheets.length - sheetIndex].setName(value)
} else {
SpreadsheetApp.getActiveSpreadsheet().insertSheet(value);
}
}
function onEdit(e) {
var sheet = e.source.getActiveSheet();
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
// Check if the sheet being edited is the first sheet and first column (A)
if (sheet.getSheetId() == sheets[0].getSheetId() && sheet.getActiveCell().getColumn() == 1) {
createSheet(e.range.getRowIndex(), e.value)
}
}
You can extend the code to support deletion;可以扩展代码支持删除; it's a bit tricky though because there is no onDelete event.
这有点棘手,因为没有 onDelete 事件。 You can compare the sheets array with the values in the column to determine which sheet delete.
您可以将工作表数组与列中的值进行比较,以确定删除哪个工作表。
Creating Multiple Sheets with Templates and Position使用模板和 Position 创建多个工作表
function onOpen() {
SpreadsheetApp.getUi().createMenu("My Menu")
.addItem('Create onOpen Trigger', 'createTrigger')
.addToUi();
}
//This is where the sheets get created
function startUp() {
var ss=SpreadsheetApp.getActive();
var templateA=["A","B","C"];
templateA.forEach(function(name){ss.getSheetByName(name).hideSheet();});
var sh=ss.getSheetByName("Sheet Data");
var shts=ss.getSheets();
var shtnames=shts.map(function(sh){if(!sh.isSheetHidden()){return sh.getName();}}).filter(function(e){return e});
var hdnshts=shts.map(function(sh){if(sh.isSheetHidden()){return sh.getName();}}).filter(function(e){return e});
var rg=sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn());
var vA=rg.getValues();
vA.sort(function(a,b){return a[2]-b[2];});
vA.forEach(function(r){
if(shtnames.indexOf(r[0])==-1 && r[0]!=sh.getName()) {
ss.insertSheet(r[0], r[2], {template:ss.getSheetByName(r[1])});
}
shts=ss.getSheets();
shtnames=shts.map(function(sh){if(!sh.isSheetHidden()){return sh.getName();}}).filter(function(e){return e});
});
var end="is near";
}
function isTrigger(funcName){
var r=false;
if(funcName){
var allTriggers=ScriptApp.getProjectTriggers();
for(var i=0;i<allTriggers.length;i++){
if(funcName==allTriggers[i].getHandlerFunction()){
r=true;
break;
}
}
}
return r;
}
//Installable onOpen trigger
//run this function first
function createTrigger() {
var ss=SpreadsheetApp.getActive();
if(!isTrigger('startUp')) {//This keeps you from creating unnecessary triggers
ScriptApp.newTrigger('startUp').forSpreadsheet(ss).onOpen().create()
}
}
//This is useful for testing
function deleteSheets() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName("Sheet Data");
var rg=sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn());
var vA=rg.getValues();
vA.forEach(function(r){
ss.deleteSheet(ss.getSheetByName(r[0]));
});
}
The Creation Sheet:创作表:
Three Templates: I just made these templates up as simple examples to assist in the creation of your sheets.三个模板:我只是将这些模板制作为简单的示例,以帮助您创建工作表。 I find that non programmers really excel at this process making extremely fancy spreadsheets.
我发现非程序员在这个过程中真的是 excel 制作非常精美的电子表格。 For me, I keep them simple.
对我来说,我让它们保持简单。
I tried using this gif to show how the files are create with the spreadsheet opens up the first time.我尝试使用此 gif 来展示如何在第一次打开电子表格时创建文件。 It won't create additional sheets of the same name.
它不会创建其他同名工作表。 But if you add additional names it will create them.
但是,如果您添加其他名称,它将创建它们。 It's not capable of deleting sheet names that have been removed, so there's something you can do for yourself.
它无法删除已删除的工作表名称,因此您可以为自己做一些事情。
This function below creates a new sheet every time a cell from column A in your original sheet is edited, only if another sheet with this name does not exist already.每次编辑原始工作表中 A 列中的单元格时,下面的 function 都会创建一个新工作表,前提是不存在具有此名称的另一个工作表。 To accomplish that, and onEdit trigger is used, as well as the corresponding event object :
为此,使用了onEdit触发器以及相应的事件 object :
function onEdit(e) {
var firstSheetName = "Sheet1"; // Name of your original sheet, change accordingly
var range = e.range; // Edited cell
var ss = e.source; // Edited spreadsheet
var sheetName = range.getSheet().getName(); // Name of edited sheet
var value = range.getValue(); // Value of the edited cell
// Checks whether edited sheet is the original one, and edited column is A:
if(sheetName == firstSheetName && range.getColumn() == 1) {
// Checks whether a sheet with this name exists:
var sheetExists = ss.getSheets().some(function(sheet) {
return sheet.getName() == value;
});
// Create new sheet only if a sheet with this name it doesn't exist:
if(!sheetExists) {
ss.insertSheet(value);
range.activate(); // Go back to original edited cell
}
}
}
I hope this is of any help.我希望这有任何帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.