javascript 如何加快谷歌表格中的脚本?

dw1jzc5e  于 2023-01-11  发布在  Java
关注(0)|答案(1)|浏览(153)

有一个表格,其中有许多工作表。一些单元格填充了值,一些单元格填充了公式。需要阻止更改包含公式的单元格。它们需要仅对有限数量的用户可用。我设法编写了此脚本,但它运行速度不快。有什么方法可以加快它吗?

function myFunction2() {
const sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
 
for(const sheeto of sheets) { //проводим перебор всех листов
 
 var ss1 = sheeto.getName();
 
 var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(ss1);
 var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE); 
 for (var i = 0; i < protections.length; i++) { //удаляем имеющиеся блокировки при наличии
   var protection = protections[i];
   if (protection.canEdit()) {
     protection.remove();
   }
 }
 var arr2 = ss.getDataRange().getFormulas();
 var numRows = arr2.length-1;
 var numCols = arr2[0].length-1;
 for (var i = 0; i <= numCols; ++i) {
   for (var y = 0; y <= numRows; ++y) {
     if (arr2[y][i]!="") { //блокируем все с формулами
       var range = ss.getRange(y+1,i+1);
       var protection = range.protect().setDescription('автозащита');
       var me = Session.getEffectiveUser();
       protection.addEditor(me);
       
       protection.addEditor('пользователь1');
       protection.addEditor('пользователь2');
       protection.addEditor('пользователь3');
       protection.removeEditors(protection.getEditors());
       if (protection.canDomainEdit()) {
         protection.setDomainEdit(false);

       }
     }
   }
 }
}
}

我尝试直接在protections数组中使用命名范围或分配阻塞范围,但在这种情况下,我的代码出现错误,无法完全工作。

67up9zun

67up9zun1#

一般来说,我发现改变方法。我块不是单独的范围,而是整个工作表。没有公式的单元格,这应该增加到变化,存储在一个数组中。我把这个数组作为排除范围的清单块。与这种方法,它证明了快得多。

function myFunction2() {
const sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  
for(const sheeto of sheets) {
  
  var ss1 = sheeto.getName();
  if  (ss1!="Лист1"  ) {
  
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(ss1);
  

  var arr2 = ss.getDataRange().getFormulas();
  

  var numRows = arr2.length-1;
  var numCols = arr2[0].length-1;

var Range1 = new Array(0);
var Range2 = new Array(0);
var Range3 = new Array(1);
vyh: {
  for (var i = 3; i <= numRows; ++i) {
    for (var y = 3; y <= numCols; ++y) {
      if (arr2[i][y]!="") {
        
        
       

      }
      else{
        yy=y+1
        ii=i+1
        Range1.length=Range1.length+1
        Range1[Range1.length-1]=yy 
        Range2.length=Range2.length+1
        Range2[Range2.length-1]=ii 

      }  
    }
  }

}

Range3[0]=col2A1(Range1[0]) + Range2[0]
ii=1
f=0
for (var i = 1; i <= Range1.length-1; ++i){

  if (Range2[i]==Range2[i-1] && Range1[i-1]==Range1[i]-1) {
    if (f==0) {
    Range3[ii-1] = Range3[ii-1] + ":" + col2A1(Range1[i]) + Range2[i]
    f=1
    }
    else{
      Range3[ii-1]=Range3[ii-1].slice(0, Range3[ii-1].indexOf(":")) + ":" + col2A1(Range1[i]) + Range2[i]
      
    }
  }
  else {
    Range3[ii]=col2A1(Range1[i]) + Range2[i]
    ii=ii+1
    f=0

  }

 }


var protection = ss.protect().setDescription('автозащита листа');

var me = Session.getEffectiveUser();
protection.addEditor(me);

protection.removeEditors(protection.getEditors());
if (protection.canDomainEdit()) {
  protection.setDomainEdit(false);
}
var protection = ss.protect();

var unprotected = protection.getUnprotectedRanges();

 for (var i = 0; i <= Range3.length-1; ++i){
unprotected.push(ss.getRange(Range3[i]));
protection.setUnprotectedRanges(unprotected);

 }
 }
}
}  

function col2A1(col){
  let result = "";
  let base = 27;
  while(col>0){    
    let newLetter = String.fromCharCode("A".charCodeAt(0)+(col-1)%(base-1));  
    result=newLetter + result;
    col = (col - col%base) / base;
  };
  return result;
};

相关问题