简体   繁体   English

Google 表格应用程序脚本编辑受保护的单元格或 Object 错误

[英]Google Sheets App Script Edit Protected Cell or Object Error

I have a Google Sheets script that I've been using for the past 2+ years that grabs a chunk of data and transcribes it into a pair of tally sheets, then clears the cells that had been filled creating said chunk of data so the process can be started over.我有一个谷歌表格脚本,我在过去 2 年多的时间里一直在使用该脚本,它抓取一大块数据并将其转录成一对计数表,然后清除已填充的单元格,创建所述数据块,以便处理可以重新开始。 This is used for an inventory calculation system.这用于库存计算系统。 The script has reliably worked up until roughly 3 weeks ago, but I now encounter an edit protection error when users with limited access to the sheet attempt to run the script.直到大约 3 周前,该脚本一直可靠运行,但是当对工作表具有有限访问权限的用户尝试运行该脚本时,我现在遇到了编辑保护错误。 None of the editable cells/ranges reference in the script are locked to any user, but the sheet does have protection on cells I do not want anybody to make inadvertent changes to.脚本中没有任何可编辑的单元格/范围引用被锁定给任何用户,但工作表确实对单元格有保护,我不希望任何人无意中更改。

The script is:脚本是:

function CopyErADD() {
 var sss=SpreadsheetApp.getActiveSpreadsheet();
 var sheet = sss.getSheetByName('ADDER'); //Entry Sheet
 var rangeIn = sheet.getRange('B32:N45'); //Range to copy into "Incoming" sheet
 var dataIn = rangeIn.getValues();
 var ts = sss.getSheetByName('Incoming'); //Tally sheet in
 
  ts.getRange(ts.getLastRow()+1, 1, dataIn.length, dataIn[0].length).setValues(dataIn);

 var rangeOut = sheet.getRange('B48:O54'); //Range to copy into "Outgoing" sheet
 var dataOut = rangeOut.getValues();
 var tss = sss.getSheetByName('Outgoing'); //Tally sheet out
 
  tss.getRange(tss.getLastRow()+1, 1, dataOut.length, dataOut[0].length).setValues(dataOut);

   SpreadsheetApp.flush() // NEWLY ADDED PER METAMAN'S SUGGESTION

  sheet.getRange('E2:E5').clearContent();
  sheet.getRange('B7:B20').clearContent();
  sheet.getRange('E7:H20').clearContent();
  sheet.getRange('I7:I20').setValue('kg');
  sheet.getRange('L7:L20').clearContent();
  sheet.getRange('B24:B29').clearContent();
  sheet.getRange('J24:J29').clearContent();
}

I also have an "erase only" script that runs the second second part of the script only (if data has been entered incorrectly) that executes perfectly fine.我还有一个“仅擦除”脚本,它只运行脚本的第二部分(如果数据输入不正确),执行得非常好。 It is only when coupled with the copy/transcribe portion of the script that the protection error occurs.只有在与脚本的复制/转录部分结合使用时,才会发生保护错误。

function ErADD() {
 var sss=SpreadsheetApp.getActiveSpreadsheet();
 var sheet = sss.getSheetByName('ADDER');
  
  sheet.getRange('E2:E5').clearContent();
  sheet.getRange('B7:B20').clearContent();
  sheet.getRange('E7:H20').clearContent();
  sheet.getRange('I7:I20').setValue('kg');
  sheet.getRange('L7:L20').clearContent();
  sheet.getRange('B24:B29').clearContent();
  sheet.getRange('J24:J29').clearContent();
}

Commenting out the sheet.getRange.... etc clearContent and setValue portion of the first script allows it to complete successfully, so I attempted to create a master function that calls the CopyErAdd script (sans clear portion) and then calls the ErADD script, and I encounter the same error.注释掉 sheet.getRange.... 等第一个脚本的 clearContent 和 setValue 部分允许它成功完成,所以我尝试创建一个主 function 调用 CopyErAdd 脚本(无清除部分),然后调用 ErADD 脚本,我遇到了同样的错误。 Both script can be run on their own successfully, but when combined the erase portion encounters the error.两个脚本都可以单独成功运行,但是当组合擦除部分时会遇到错误。

Does anybody see any issues that I am missing, or did something occur over the past few weeks that I'm not aware of that could cause this protection error?是否有人看到我遗漏的任何问题,或者在过去几周是否发生了我不知道的可能导致此保护错误的事情?

I appreciate any ideas anybody might have.我很欣赏任何人可能有的任何想法。

Edit - Thank you MetaMan for the tips for making the script more efficient.编辑- 感谢 MetaMan 提供的使脚本更高效的提示。

As for the protection, "Incoming" and "Outgoing" copy destination sheets are completely open, no protection at all.至于保护,“传入”和“传出”复制目标表是完全打开的,根本没有保护。 "ADDER" sheet is protected except certain cells that are edited by user, and are referenced in the script.除了用户编辑并在脚本中引用的某些单元格外,“加法器”工作表受到保护。 E2:E5 (actually E2:I5 open, since the macro button sits on top of the F2:I5 range), B7:B20, E7:I20, L7:L20, B24:B29, J24:J29, All unprotected. E2:E5(实际上是 E2:I5 打开,因为宏按钮位于 F2:I5 范围的顶部)、B7:B20、E7:I20、L7:L20、B24:B29、J24:J29,全部不受保护。

Adding the line添加行

SpreadsheetApp.flush()

in between the copy section and clearing section of the code worked (as shown in the updated code snippet).在代码的复制部分和清除部分之间工作(如更新的代码片段所示)。 I'm no longer encountering a range protection error.我不再遇到范围保护错误。

I really appreciate the fix, but I'm boggled over why I've never needed that line until recently.我真的很感激这个修复,但我很困惑为什么直到最近我才需要这条线。 I guess you don't need flush() until you do.我猜你不需要 flush() 直到你这样做。

You could replace this:你可以替换这个:

  sheet.getRange('I7').setValue('kg');
  sheet.getRange('I8').setValue('kg');
  sheet.getRange('I9').setValue('kg');
  sheet.getRange('I10').setValue('kg');
  sheet.getRange('I11').setValue('kg');
  sheet.getRange('I12').setValue('kg');
  sheet.getRange('I13').setValue('kg');
  sheet.getRange('I14').setValue('kg');
  sheet.getRange('I15').setValue('kg');
  sheet.getRange('I16').setValue('kg');
  sheet.getRange('I17').setValue('kg');
  sheet.getRange('I18').setValue('kg');
  sheet.getRange('I19').setValue('kg');
  sheet.getRange('I20').setValue('kg');

with this:有了这个:

  sheet.getRange('I7:I20).setValue('kg');

also replace this:也替换这个:

 var ss = sss.getSheetByName('ADDER'); //Entry sheet
 var sheet = SpreadsheetApp.getActive().getSheetByName('ADDER');

with this:有了这个:

 var sheet = sss.getSheetByName('ADDER);

If you are doing anything immediate after this function with the data then you might wish to add SpreadsheetApp.flush()如果您在此 function 之后立即使用数据执行任何操作,那么您可能希望添加SpreadsheetApp.flush()

I can't comment on the protection since none of that information was provided.我无法评论保护,因为没有提供任何信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编辑谷歌表格应用脚本中过滤行中的单元格值 - Edit cell value from filtered row in app script for google sheets Google App脚本-如何允许其他用户通过脚本编辑仅由我保护的单元格? - Google App Script - How to allow other users edit only-me-protected cell through script? 编辑名为 function 的单元格上方的单元格? - 谷歌表格应用脚本 - Edit cell above cell that called a function? - Google Sheets apps script 谷歌表格脚本:搜索任何有错误的单元格 - Google Sheets Script: Search For Any Cell With Error 谷歌表格脚本出错 - 将公式插入单元格 - Error with Google Sheets script – insert formula into cell 脚本 Google 表格错误,因为单元格为空 - Script Google Sheets error because cell is empty 使用应用脚本获取给定谷歌表格中单元格值的单元格 - Getting the cell given the cell value in google sheets using app script Google Sheets Apps脚本处于“编辑”状态时,从受保护范围中删除编辑器的速度非常慢/未完成 - Google Sheets Apps Script On Edit Remove Editors From Protected Range Very Slow / Not Completing 如何运行 Google 表格应用脚本,如果出现错误,则调用电子表格 function IMPORTXML() 来填充单元格? - How to run a Google Sheets App Script, and if there is an error, then call Spreadsheet function IMPORTXML() instead to populate the cell? Google表格应用脚本的神秘错误 - Google Sheets App Script Mysterious Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM