1

任何人都可以简化和重新排列我的脚本,使其运行得更快而不会卡住。该脚本用于从 4 个电子表格中提取电子表格数据并根据列标准删除空白行,该脚本运行良好,但需要很长时间,并且突然/经常中断进程,说电子表格页面需要刷新,从而停止整个过程。这应该遵循重新运行脚本。- 提前致谢。

我的脚本如下:

function myFunction() {
// spreadsheets key to extract data.
//Bhaskar      0AjkkHlm3kCphdFh3M2dtRDdoZHhWZlg5UzFjSWZFcVE
//RAmohan      0AjkkHlm3kCphdGFlNTVhSDc5VXVTeS0xc2ZISDRGZlE
//Krishnareddy 0AjkkHlm3kCphdEhDWEllalpoM3VmNE5weUNLZkd4TVE
//Suni          0AjkkHlm3kCphdFUxTXJlN3lGYkRMY2NXUUkzSTVfbEE

    var copieddata = SpreadsheetApp.openById('0AjkkHlm3kCphdFh3M2dtRDdoZHhWZlg5UzFjSWZFcVE').
                    getSheetByName('List').getDataRange().getValues();
                //getSheetByName('List').getRange('I6:AT500').getValues();      

    var s1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
                SpreadsheetApp.setActiveSheet(s1);
            //getRange(StartingROWS(1,2,3,4,...),StartingCOLUMNS(A,B,C,D..),  

           s1.getRange(1,1,copieddata.length,copieddata[0].length).setValues(copieddata);


    //var col1count = SpreadsheetApp.getActiveSheet().getRange(1,1,copieddata.length,copieddata[0].length).getNumColumns();
    //var row1count = SpreadsheetApp.getActiveSheet().getRange(1,1,copieddata.length,copieddata[0].length).getNumRows();

SpreadsheetApp.flush(); //force the data to be written
// here ends sheet 1  -------

    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    var numRows  = sheet.getLastRow(); 
    var datasheet  = sheet.getDataRange();
    var data = datasheet.getValues();

//Browser.msgBox(" data lenght -" + data.length + " rage is " + datasheet);

    for (var i = data.length; i > 0; --i){ 
        var ii=i-1;
        var row = data[ii]; 
        var name = row[6]; 
        var len = name.length;

        //Browser.msgBox("row number" + i + " ->" + row[10] + "<- its length =" + len);

        if(name.length!=3){ 
            //Browser.msgBox("delete ." + i);
            sheet.deleteRow(i);
        } 
   SpreadsheetApp.flush(); //force the data to be written
     } 
   SpreadsheetApp.flush(); //force the data to be written


// ------------------


    var lrange = SpreadsheetApp.getActiveSheet().getLastRow();

//Browser.msgBox("l range - " + lrange); 

    var copieddata = SpreadsheetApp.openById('0AjkkHlm3kCphdGFlNTVhSDc5VXVTeS0xc2ZISDRGZlE').
                    getSheetByName('List').getDataRange().getValues();

    var s1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
                SpreadsheetApp.setActiveSheet(s1);

        s1.getRange((lrange+1),1,copieddata.length,copieddata[0].length).setValues(copieddata);


    //var col1count = SpreadsheetApp.getActiveSheet().getRange(1,1,copieddata.length,copieddata[0].length).getNumColumns();
    //var row1count = SpreadsheetApp.getActiveSheet().getRange(1,1,copieddata.length,copieddata[0].length).getNumRows();

SpreadsheetApp.flush(); //force the data to be written
// here ends sheet 2  -------

    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    var numRows  = sheet.getLastRow(); 
    var datasheet  = sheet.getDataRange();
    var data = datasheet.getValues();

//Browser.msgBox(" data lenght -" + data.length + " rage is " + datasheet);

    for (var i = data.length; i > 0; --i){ 
        var ii=i-1;
        var row = data[ii]; 
        var name = row[6]; 
        var len = name.length;

        //Browser.msgBox("row number" + i + " ->" + row[10] + "<- its length =" + len);

        if(name.length!=3){ 
            //Browser.msgBox("delete ." + i);
            sheet.deleteRow(i);
        } 
   SpreadsheetApp.flush(); //force the data to be written
     } 


// ------------------


    var lrange = SpreadsheetApp.getActiveSheet().getLastRow();

//Browser.msgBox("l range - " + lrange); 

    var copieddata = SpreadsheetApp.openById('0AjkkHlm3kCphdEhDWEllalpoM3VmNE5weUNLZkd4TVE').
                    getSheetByName('List').getDataRange().getValues();

    var s1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
                SpreadsheetApp.setActiveSheet(s1);

        s1.getRange((lrange+1),1,copieddata.length,copieddata[0].length).setValues(copieddata);


    //var col1count = SpreadsheetApp.getActiveSheet().getRange(1,1,copieddata.length,copieddata[0].length).getNumColumns();
    //var row1count = SpreadsheetApp.getActiveSheet().getRange(1,1,copieddata.length,copieddata[0].length).getNumRows();

SpreadsheetApp.flush(); //force the data to be written  
// here ends sheet 3  -------

    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    var numRows  = sheet.getLastRow(); 
    var datasheet  = sheet.getDataRange();
    var data = datasheet.getValues();

//Browser.msgBox(" data lenght -" + data.length + " rage is " + datasheet);

    for (var i = data.length; i > 0; --i){ 
        var ii=i-1;
        var row = data[ii]; 
        var name = row[6]; 
        var len = name.length;

        //Browser.msgBox("row number" + i + " ->" + row[10] + "<- its length =" + len);

        if(name.length!=3){ 
            //Browser.msgBox("delete ." + i);
            sheet.deleteRow(i);
        } 
   SpreadsheetApp.flush(); //force the data to be written
     } 


// ------------------

    var lrange = SpreadsheetApp.getActiveSheet().getLastRow();

//Browser.msgBox("l range - " + lrange); 

    var copieddata = SpreadsheetApp.openById('0AjkkHlm3kCphdFUxTXJlN3lGYkRMY2NXUUkzSTVfbEE').
                    getSheetByName('List').getDataRange().getValues();

    var s1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
                SpreadsheetApp.setActiveSheet(s1);

        s1.getRange((lrange+1),1,copieddata.length,copieddata[0].length).setValues(copieddata);


    //var col1count = SpreadsheetApp.getActiveSheet().getRange(1,1,copieddata.length,copieddata[0].length).getNumColumns();
    //var row1count = SpreadsheetApp.getActiveSheet().getRange(1,1,copieddata.length,copieddata[0].length).getNumRows();

SpreadsheetApp.flush(); //force the data to be written  
// here ends sheet 4  -------

    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    var numRows  = sheet.getLastRow(); 
    var datasheet  = sheet.getDataRange();
    var data = datasheet.getValues();

//Browser.msgBox(" data lenght -" + data.length + " rage is " + datasheet);

    for (var i = data.length; i > 0; --i){ 
        var ii=i-1;
        var row = data[ii]; 
        var name = row[6]; 
        var len = name.length;

        //Browser.msgBox("row number" + i + " ->" + row[10] + "<- its length =" + len);

        if(name.length!=3){ 
            //Browser.msgBox("delete ." + i);
            sheet.deleteRow(i);
        } 
   SpreadsheetApp.flush(); //force the data to be written
     } 
}

电子表格链接如下:点击此处进入我的电子表格

4

1 回答 1

4

这是我的尝试:

function myFunction2() {
  var source = ['0AjkkHlm3kCphdFh3M2dtRDdoZHhWZlg5UzFjSWZFcVE',
                '0AjkkHlm3kCphdGFlNTVhSDc5VXVTeS0xc2ZISDRGZlE',
                '0AjkkHlm3kCphdEhDWEllalpoM3VmNE5weUNLZkd4TVE',
                '0AjkkHlm3kCphdFUxTXJlN3lGYkRMY2NXUUkzSTVfbEE'];
  var copied = [];
  for (var i = 0; i < source.length; i++) {
    copied = copied.concat(SpreadsheetApp.openById(source[i]).getSheetByName('List').getDataRange().getValues());
  }
  for (var j = 0; j < copied.length; j++) {
    if (copied[j][6].length != 3) {
      copied.splice(j, 1);
      j--;
    }
  }
  var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
  sheet.clearContents();
  sheet.getRange(1, 1, copied.length, copied[0].length).setValues(copied);
}

在此我从四个电子表格创建一个大的 javascript 数组,从该数组中删除元素(“行”)(而不是删除电子表格本身中的行,我认为这是它缓慢的原因),清除目标工作表,并设置一次修改数组的值。

HTH亚当

于 2012-06-02T23:45:56.370 回答