首先,我必须说,我的谷歌应用程序脚本的知识水平几乎为零。我试图创建使用宏实用程序的脚本,但由此产生的代码太长。我会很感激,如果有人能帮助我简化此代码。我表示物体的下面的参数:
我想要做的--using“间接”式 - 是: - 这片1的数据在指数C2被复制:M11。 - 即上板2的数据在C12被复制到索引:M21。 - 即上薄板3中的数据被复制到索引中C22:M31。 - 这片4的数据在索引被复制在C32:M41。 - 这片5的数据在索引被复制在C42:M51。 - 等等。
感谢您的关注。
function INDIRECT() {
var s = SpreadsheetApp.getActive();
s.getRange("c2").setFormula('=INDIRECT("\'"&Index!b2&"\'!b8:l17")');
s.getRange("c2").offset(10, 0).activate();
s.getCurrentCell().offset(-10, 0).copyTo(s.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
s.getCurrentCell().offset(10, 0).activate();
s.getCurrentCell().offset(-20, 0).copyTo(s.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
s.getCurrentCell().offset(10, 0).activate();
s.getCurrentCell().offset(-30, 0).copyTo(s.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
s.getCurrentCell().offset(10, 0).activate();
s.getCurrentCell().offset(-40, 0).copyTo(s.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
}
的OP需要填充一个主片与其他片材的内容。
下面的代码假定片材的名称已经填充在列B(按照OP电子表格)。
等效query
式(在细胞C3,例如)如下所示:
=query(Funda!$B$8:$L$17;" Select * ")
。
此代码不会产生自定义公式。这是可能只运行一次,通过主片的列B循环,并插入适当的查询到列C的基础上,在列B中列出的表名称的代码可能只运行一次的程序。除非有电子表格中的布局的改变,或者被添加更多片材,由代码插入查询公式将更新主片上的子片的所有更改。
function so54463600() {
// setup spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mastername = "Master"; // Change this sheet name to whatever you want
var sheet = ss.getSheetByName(mastername);
// The sheet data range
var datarange = "$B$8:$L$17";
// set the data and number of sheets in Column B
var Bvals = sheet.getRange("B1:B").getValues();
var Blast = Bvals.filter(String).length;
var lastRow = sheet.getLastRow();
//Logger.log("DEBUG: the last row = "+lastRow);//DEBUG
// loop through the rows of column B
for (var x = 0; x < lastRow; x++) {
// action if the cell has contents
if (Bvals[x][0].length != 0) {
// Logger.log("DEBUG: x = "+x+", Value = "+Bvals[x][0]+", length: "+Bvals[x][0].length);//DEBUG
// sample query: =query(Funda!$B$8:$L$17;" Select * ")
var formula = "=query(" + Bvals[x][0] + "!" + datarange + ";" + '"' + " Select * " + '")';
// Logger.log("DEBUG: the formula is "+formula);//DEBUG
// Location to set query formula
Logger.log("target range:" + sheet.getRange(x + 1, 3).getA1Notation());
// set the formula
sheet.getRange(x + 1, 3).setFormula(formula);
}
}
}
修订2019年2月4日 更新表单名称编程
在电子表格中的片材的数目可以是50或更大。表名称可以更改,片材可以被添加或删除。因此,为了保证在主片上示出的数据是完整和准确的,理想的是片材的名称来编程插入,而不是手动。
该版本包括新的子程序buildsheetnames
与一些改变原来的代码。至于这是一个完整的替代,而不是一个简单的插件。
采购工作表名称
片材名称与getSheets()
首先获得,然后getName()
。
用户组织在适合他们的屏幕顺序表;只列出到“主”片材的片材权将被包括在由该例程中显示的名称。
用户定义的变量startingsheetNumber
被包括,以限定所述第一片的“主”列表上被显示。这是通过从左至右所有工作表到右,从0开始计数获得。第一片材为“Master”的右侧的计数值应分配给该变量。张为“大师”的右的其余部分将被自动包含。
function so54463600mk2() {
// setup spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mastername = "Master";
var sheet = ss.getSheetByName(mastername);
// The sheet data range
var datarange = "$B$8:$L$17";
// select the existing sheet data and clear the contents
// do this in case a sheet is added or deleted; minimize chance of not detecting an error.
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
//Logger.log("last row = "+lastRow+", last Column: "+lastColumn);
var masterrange = sheet.getRange(2, 2, lastRow - 1, lastColumn - 1);
masterrange.clear();
// update the sheet names in column B
// run the sub-routine buildsheetnames
buildsheetnames(mastername);
// set the data and number of sheets in Column B
var Bvals = sheet.getRange("B1:B").getValues();
var Blast = Bvals.filter(String).length;
var lastRow = sheet.getLastRow();
//Logger.log("DEBUG: the last row = "+lastRow);//DEBUG
// loop through the rows of column B
for (var x = 0; x < lastRow; x++) {
// action if the cell has contents
if (Bvals[x][0].length != 0) {
// Logger.log("DEBUG: x = "+x+", Value = "+Bvals[x][0]+", length: "+Bvals[x][0].length);//DEBUG
// sample query: =query(Funda!$B$8:$L$17;" Select * ")
//var formula = "=query(" + Bvals[x][0] + "!"+datarange+";"+'"'+" Select * "+'")';
var formula = "=query(" + "'" + Bvals[x][0] + "'!" + datarange + ";" + '"' + " Select * " + '")';
// Logger.log("DEBUG: the formula is "+formula);//DEBUG
// Location to set query formula
Logger.log("target range:" + sheet.getRange(x + 1, 3).getA1Notation());
// set the formula
sheet.getRange(x + 1, 3).setFormula(formula);
}
}
}
function buildsheetnames(mastername) {
// setup spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(mastername);
// move the curcor to cell B2
var row = 2;
var column = 2;
sheet.getRange(2, 2).activate();
// get the list of sheets
var sheetlist = ss.getSheets();
// Logger.log("DEBUG: Number of sheets: "+sheetlist.length);//DEBUG
// Displaying Sheet names on the master sheet
// Instructions:
// 1 - View the spreadsheet with sheets laid out at the bottom of the screen
// 2 - Move the sheets into the order that you want;
// 2a - Sheets to the left of "Master" (or whatever sheet you define as "mastername") wouldn't be included in the sheet names written onto Master
// 2b - Only the names of those sheets to the right of "Master" will be displayed.
// 3 - Count ALL the sheets, from left to right of screen. The first sheet will be zero, the second sheet will be one, and so on.
// 4 - Stop when you reach the first sheet that you want the name displayed on Master
// 5 - Update the count value for that sheet to the variable 'startingsheetNumber'.
// 6 - Note: the variable "sheetrowseparation" is the number of rows between each name;
// 6a - This is the same as the number of data rows on each data sheet.
// 6b - If the number of data rows ever changes, then you can change this variable.
var startingsheetNumber = 6;
var sheetrowseparation = 10;
// loop through the sheets
if (sheetlist.length > 1) {
for (var i = startingsheetNumber; i < sheetlist.length; i++) {
//Logger.log("DEBUG: Sheet#: "+i+", Sheet name: "+sheetlist[i].getName());//DEBUG
sheet.getRange(row, column).setValue(sheetlist[i].getName());
row = row + sheetrowseparation;
}
}
return;
}