在所有工作表中添加新行的脚本
我将非常感谢您的帮助! 我有一张电子表格,里面有4张相同的表格,它们有相同的数据模板(这张表中的所有表格都是相同的(大小,名称,ets),只有表格中的数据是唯一的,表格名称也是其他表格)名称这4张。 它看起来像这样: https://docs.google.com/spreadsheets/d/1Xdws6Vr6YVXuj19mvpOvkpJokOfY2T7Sh1aMpous_Xw/edit#gid=0在所有工作表中添加新行的脚本
我想要做什么: 在所有4张在同一个地方添加行的同一数字。 例如,在行A11后的工作表“1. Win”中添加10行并插入下个月的日期(在示例中它必须为01.07.2014等等)。对剩余的纸张重复此操作。
我有一些流行的脚本,它创建弹出式窗口,我可以键入一些行数,然后在活动单元格中添加行。
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [{name: "Add rows", functionName: "doGet"}];
ss.addMenu("Bonus script", menuEntries);
}
function doGet(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var app =UiApp.createApplication().setTitle('Insert Rows').setHeight(75).setWidth(225);
// Create a grid with 1 text box and corresponding label.
// Test entered into the text box is passed in to numRows.
// The setName extension will make the widget available by the given name to the server handlers later.
var grid = app.createGrid(1, 2);
grid.setWidget(0, 0, app.createLabel('Number of Rows to Insert:'));
grid.setWidget(0, 1, app.createTextBox().setName('numRows').setWidth(50));
// Create a Vertical Panel and add the Grid to the Panel.
var panel = app.createVerticalPanel();
panel.add(grid);
// Create a button and Click Handler.
// Pass in the Grid Object as a callback element and the handler as a click handler.
// Identify the function insertRows as the server click handler.
var button = app.createButton('Submit');
var handler = app.createServerHandler('insertRows');
handler.addCallbackElement(grid);
button.addClickHandler(handler);
// Add the button to the Panel, add Panel to the App, launch the App
panel.add(button);
app.add(panel);
ss.show(app);
}
function insertRows(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cursPos = sheet.getActiveCell().getRow();
var valueRows = e.parameter.numRows;
sheet.insertRowsAfter(cursPos, valueRows);
var app = UiApp.getActiveApplication();
app.close();
return app;
}
但我不明白如何改造它满足我的需求。我有一些想法,不要(在表“All_base”为前A1)使用弹出窗口,行的型号在一些细胞,用于valueRows。
它的丑陋的部分是找到一行后,你应该插入新的行。我从第6行开始,然后寻找说“图片”的单元格,因为您在每张表中都有。然后,插入它上面的行2个位置。
它可以通过查找最后一个日期类型的位置来完成,但是我不确定您的图纸在空白时如何显示,因为它们中有许多空行。
添加10行并插入下个月日期
这是否意味着你想以1为您添加的每一行增加一个月?如果不是,则将newDate.setMonth(newDate.getMonth()+1+i);
更改为newDate.setMonth(newDate.getMonth()+1);
。
function loop(howMany){
if(!howMany){howMany = 2;};
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();//returns an array of all sheets
//this is just a different way of looping through the sheets
for (var sheetId = 0; sheetId<sheets.length; sheetId++){
if(sheets[sheetId].getSheetName() != "All_base"){
var data = sheets[sheetId].getDataRange().getValues();
var insertAfter = 6;
for(var i = 5; i<data.length; i++){
if(data[i][0] == "Picture"){
insertAfter = i-1; //it is actually -2, but since i starts from 0, it is already smaller by 1;
//actual row of Picture cell is i+1, and 2 cells up is i+1-2 = i-1
};
};
sheets[sheetId].insertRowsAfter(insertAfter, howMany);
//At this point we inserted rows
var lastDate = sheets[sheetId].getRange(insertAfter,1).getCell(1,1).getValue();//the last specified date as a Date object
for(var i=0; i<howMany; i++){
var newDate = new Date(lastDate.getTime());
newDate.setMonth(newDate.getMonth()+1+i);//automatically will increase year if the next month is in the next year.
sheets[sheetId].getRange(insertAfter+1+i,1).getCell(1,1).setValue(newDate);
};
};
};
}
如果你想添加的搜索引擎优化和直接值的循环,你可以做到这一点通过增加
sheets[sheetId].getRange(insertAfter+1+i,2).getCell(1,1).setValue(seo);
sheets[sheetId].getRange(insertAfter+1+i,3).getCell(1,1).setValue(direct);
的循环,其中seo
和direct
是您要添加的值,或加入
sheets[sheetId].getRange(insertAfter+1+i,1,1,3).setValues([[newDate,seo,direct]]);
注:这将创建一个没有风格行。
Amayzing,它的工作原理!谢谢Ihor Husar!对不起,很长时间以来,病情很重,在医院度过了3个月 – Twil
刚开始测试,并找到一些,我想改善。我添加到一张工作表中https://docs.google.com/spreadsheets/d/1Xdws6Vr6YVXuj19mvpOvkpJokOfY2T7Sh1aMpous_Xw/edit#gid=1218307973带有“图片”的更多单元格,并启动脚本。它只适用于表单中最后一个“图片”行,如果我有更多的“图片”单元格,它会错过。如何为所有的“图片”单元格? – Twil
在此处添加问题http://ru.stackoverflow.com/questions/461928/%D0%A1%D0%BA%D1%80%D0%B8%D0%BF%D1%82-%D0%B4%D0% BB%D1%8F-%D0%B4%D0%BE%D0%B1%D0%B0%D0%B2%D0%BB%D0%B5%D0%BD%D0%B8%D1%8F-%D1% 81%D1%82%D1%80%D0%BE%D0%BA-%D0%B2%D0%BE-%D0%B2%D1%81%D0%B5%D1%85-%D0%B2%D0 %BA%D0%BB%D0%B0%D0%B4%D0%BA%D0%B0%D1%请注意,真的需要你的帮助 – Twil