Google表格清除/删除过滤器的脚本

时间:2016-07-08 15:14:14

标签: javascript google-apps-script google-sheets google-sheets-api

我在Google表格中有一个脚本,可以在打开时清除/删除工作表的所有选项卡上的过滤器。它通过抓住顶行,然后删除它然后在没有过滤器的情况下替换它来完成此操作。

该脚本有效,但Sheets似乎要处理6个标签。当它运行时,它会留下2-3个完全灰色的标签,而不显示任何数据。双击灰色单元格时,它将返回以下消息:“这些单元格当前正在加载。请在加载完成时重试”。无论我等多久,数据都不会显示。如果我再次刷新页面3或4次,则所有选项卡最终都会显示数据。

我希望能够清理代码或想出一个新脚本,以便不必刷新页面。我在一张只有两个标签的工作表上使用了该脚本,并且工作正常,所以我认为这与数据量没有关系。

公平警告:在这方面我是一个极端的初学者,所以我确信代码和格式非常难看。

这是指向工作表副本的链接:

https://docs.google.com/spreadsheets/d/1cg4wVIHPumahQgf5gorc4JQXsG_6l4eg3OhONABx8gs/edit#gid=369791415

这是脚本的代码(它是每个选项卡的重复代码块):

function myFunction() 
{
var row2 = 1 //the row with filter 
var rowBefore2 = row2 
var Sheet2 = SpreadsheetApp.getActiveSpreadsheet();

SpreadsheetApp.setActiveSheet(Sheet2.getSheets()[1]);

Sheet2.insertRowBefore(row2); //inserts a line before the filter
row2++;

var Line2 = Sheet2.getRange(row2 + ":" + row2); //gets the filter line
Line2.moveTo(Sheet2.getRange(rowBefore2 + ":" + rowBefore2)); //move to
new line
Sheet2.deleteRow(row2); //deletes the filter line - this clears the 
filter

var row3 = 1
var rowBefore3 = row3 
var Sheet3 = SpreadsheetApp.getActiveSpreadsheet();

SpreadsheetApp.setActiveSheet(Sheet3.getSheets()[2]);

Sheet3.insertRowBefore(row3);
row3++;

var Line3 = Sheet3.getRange(row3 + ":" + row3);
Line3.moveTo(Sheet3.getRange(rowBefore3 + ":" + rowBefore3));
Sheet3.deleteRow(row3);

var row4 = 1 //the row with filter 
var rowBefore4 = row4 
var Sheet4 = SpreadsheetApp.getActiveSpreadsheet();

SpreadsheetApp.setActiveSheet(Sheet4.getSheets()[3]);

Sheet4.insertRowBefore(row4);
row4++;

var Line4 = Sheet4.getRange(row4 + ":" + row4);
Line4.moveTo(Sheet4.getRange(rowBefore4 + ":" + rowBefore4));
Sheet4.deleteRow(row4);

var row5 = 1 //the row with filter 
var rowBefore5 = row5 
var Sheet5 = SpreadsheetApp.getActiveSpreadsheet();

SpreadsheetApp.setActiveSheet(Sheet5.getSheets()[4]);

Sheet5.insertRowBefore(row5);
row5++;

var Line5 = Sheet5.getRange(row5 + ":" + row5);
Line5.moveTo(Sheet5.getRange(rowBefore5 + ":" + rowBefore5));
Sheet5.deleteRow(row5);

var row6 = 1
var rowBefore6 = row6 
var Sheet6 = SpreadsheetApp.getActiveSpreadsheet();

SpreadsheetApp.setActiveSheet(Sheet6.getSheets()[5]);

Sheet6.insertRowBefore(row6);
row6++;

var Line6 = Sheet6.getRange(row6 + ":" + row6);
Line6.moveTo(Sheet6.getRange(rowBefore6 + ":" + rowBefore6));

Sheet6.deleteRow(row6);

var row7 = 1 
var rowBefore7 = row7 
var Sheet7 = SpreadsheetApp.getActiveSpreadsheet();

SpreadsheetApp.setActiveSheet(Sheet7.getSheets()[6]);

Sheet7.insertRowBefore(row7);
row7++;

var Line7 = Sheet7.getRange(row7 + ":" + row7);
Line7.moveTo(Sheet7.getRange(rowBefore7 + ":" + rowBefore7));

Sheet7.deleteRow(row7);

}

2 个答案:

答案 0 :(得分:1)

遍历工作表而不是直接引用它们。然后坚持你想要做的代码...它应该看起来像这样。我复制了你的工作表并将代码放在那里并将其分享给你。它会在几秒钟内完成,并且有灰色页面。我无法访问您正在进行导入范围的工作表。因此,如果您发现此脚本仍然是灰色的,则可能与此问题有关。

function removeFilters() {
    var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
    var row1 = 1;
    var lastColumn = 20
    for (var i = 0; i < sheets.length ; i++ ) {
        var sheet = sheets[i];
      sheet.activate();
      lastColumn = sheet.getLastColumn()
      sheet.insertRowBefore(row1); //inserts a line at the top
      sheet.getRange(row1+1, 1, 1, lastColumn).moveTo(sheet.getRange("a1"));
      Logger.log("value of i: " + i + " and active sheet is: " + sheet.getName());
      sheet.deleteRow(row1+1);
        }       

}

答案 1 :(得分:0)

获取所有工作表的数组并循环显示它们:

function myFunction() {

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets =ss.getSheets() //get array of all sheets
  for(i=0;i<sheets.length;i++){ //for each sheet
 var row2 = 1 //the row with filter 
 var rowBefore2 = row2 
 var s=SpreadsheetApp.setActiveSheet(ss.getSheets()[i]);

s.insertRowBefore(row2); //inserts a line before the filter
row2++;

var Line2 = s.getRange(row2 + ":" + row2); //gets the filter line
Line2.moveTo(s.getRange(rowBefore2 + ":" + rowBefore2)); //move to new line
s.deleteRow(row2); //deletes the filter line - this clears the filter
  }}