Google apps script Google Sheets-使用工作表保护运行脚本

Google apps script Google Sheets-使用工作表保护运行脚本,google-apps-script,google-sheets,google-sheets-formula,rows,Google Apps Script,Google Sheets,Google Sheets Formula,Rows,我的谷歌表有问题。在owner视图中,一切都很好,但当我试图将其共享给编辑器时,某些功能并没有按照我的计划进行。作为所有者,我使用受保护的工作表和范围功能。因此,我使用了“保护表”,然后使用“排除某些单元格”功能,以便编辑器可以在该范围内进行编辑。我放置了两个按钮来隐藏(对于空的行)和显示行。脚本在“所有者”视图上运行良好,但当我将其打开到“编辑器”视图时,出现了一个错误,上面写着“异常:您正在尝试编辑受保护的单元格或对象。如果需要编辑,请与电子表格所有者联系以删除保护。”。除某些单元格外,中的

我的谷歌表有问题。在owner视图中,一切都很好,但当我试图将其共享给编辑器时,某些功能并没有按照我的计划进行。作为所有者,我使用受保护的工作表和范围功能。因此,我使用了“保护表”,然后使用“排除某些单元格”功能,以便编辑器可以在该范围内进行编辑。我放置了两个按钮来隐藏(对于空的行)和显示行。脚本在“所有者”视图上运行良好,但当我将其打开到“编辑器”视图时,出现了一个错误,上面写着“异常:您正在尝试编辑受保护的单元格或对象。如果需要编辑,请与电子表格所有者联系以删除保护。”。除某些单元格外,中的范围仅在F列中。我该怎么处理这个

我正在使用此代码隐藏雇员行。
var startRow=9;
var colToCheck=2;//B栏
函数shouldHideRow(ss、rowIndex、rowValue){
如果(rowValue!='')返回false;
if(ss.getRange(startRow+rowIndex,colToCheck,1,1).isPartOfMerge())返回false;
if(ss.getRange(startRow+rowIndex+1,colToCheck,1,1).isPartOfMerge())返回false;
返回true;
}
函数HideRows(){
var ss=SpreadsheetApp.getActiveSheet();
var numRows=ss.getLastRow();
var elements=ss.getRange(startRow、colToCheck、numRows).getValues();
对于(变量i=0;i numRows)
ss.hideRows(numRows+1,总numRows-numRows);

}
我相信你的目标如下

  • 在电子表格中,将使用受保护的工作表和范围
  • 您希望在非所有者的用户运行脚本时运行脚本。
    • 在当前版本中,当用户运行脚本时,受保护范围内会发生错误
修改点: 我认为解决问题的方法总是以所有者身份运行脚本。因此,在这种情况下,我建议使用Web应用程序。在这种情况下,我认为这可能是相同的情况。但是从您的脚本来看,我认为将线程反映到您的脚本中作为方法可能有点困难。因此,我想提出修改后的脚本作为答案

在这个答案中,为了以电子表格所有者的身份运行脚本,将使用Web应用程序

用法: 首先,请从共享电子表格中删除包含的气体项目
doGet

1.准备脚本。 请将以下脚本复制粘贴到脚本编辑器并保存

function doGet(e) {
  this[e.parameter.run](e.parameter.sheetName || null);
  return ContentService.createTextOutput();
}

function HideRows() {
  const activeSheet = SpreadsheetApp.getActiveSheet();
  const url = ScriptApp.getService().getUrl();
  UrlFetchApp.fetch(url + "?run=script_HideRows&sheetName=" + activeSheet.getSheetName(), {headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()}});

// DriveApp.getFiles()  // This is used for automatically detecting the scope of "https://www.googleapis.com/auth/drive.readonly". This scope is used for the access token.
}

function showRows() {
  const url = ScriptApp.getService().getUrl();
  UrlFetchApp.fetch(url + "?run=script_showRows", {headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()}});
}

var startRow = 6;
var colToCheck = 2; // Column L

// This script is the same with your "HideRows".
function script_HideRows(sheetName) {
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
  var numRows = ss.getLastRow();
  var elements = ss.getRange(startRow, colToCheck, numRows).getValues();
 
  for (var i=0; i<(numRows - startRow); i++) {
    if (shouldHideRow(ss, i, elements[i][0])) {
      ss.hideRows(startRow + i);
    }
  }
  // Hide the rest of the rows
  var totalNumRows = ss.getMaxRows();
  if (totalNumRows > numRows)
    ss.hideRows(numRows+1, totalNumRows - numRows);
};

// This script is the same with your "showRows".
function script_showRows() {
  // set up spreadsheet and sheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
//  var ss = SpreadsheetApp.getActiveSpreadsheet(),
  var sheets = ss.getSheets();

  for(var i = 0, iLen = sheets.length; i < iLen; i++) {
    // get sheet
    var sh = sheets[i];

    // unhide columns
    var rCols = sh.getRange("1:1");
    sh.unhideColumn(rCols);

    // unhide rows
    var rRows = sh.getRange("A:A");
    sh.unhideRow(rRows);
  }
};

function shouldHideRow(ss, rowIndex, rowValue) {
  if (rowValue != '') return false;
  if (ss.getRange(startRow + rowIndex, colToCheck, 1, 1).isPartOfMerge()) return false;
  if (ss.getRange(startRow + rowIndex + 1, colToCheck, 1, 1).isPartOfMerge()) return false;
  return true;
}
函数doGet(e){
此[e.parameter.run](e.parameter.sheetName | | null);
返回ContentService.createTextOutput();
}
函数HideRows(){
const activeSheet=SpreadsheetApp.getActiveSheet();
const url=ScriptApp.getService().getUrl();
UrlFetchApp.fetch(url+“?run=script_HideRows&sheetName=“+activeSheet.getSheetName(),{标题:{授权:“承载者”+ScriptApp.getOAuthToken()});
//DriveApp.getFiles()//用于自动检测“”的范围https://www.googleapis.com/auth/drive.readonly。此作用域用于访问令牌。
}
函数showRows(){
const url=ScriptApp.getService().getUrl();
fetchapp.fetch(url+“?run=script_showRows”,{headers:{授权:“承载者”+ScriptApp.getOAuthToken()}});
}
var startRow=6;
var colToCheck=2;//L列
//此脚本与您的“隐藏窗口”相同。
函数脚本_HideRows(sheetName){
var ss=SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var numRows=ss.getLastRow();
var elements=ss.getRange(startRow、colToCheck、numRows).getValues();
对于(变量i=0;i numRows)
ss.hideRows(numRows+1,总numRows-numRows);
};
//此脚本与您的“showRows”相同。
函数脚本_showRows(){
//建立电子表格和工作表
var ss=SpreadsheetApp.getActiveSpreadsheet();
//var ss=SpreadsheetApp.getActiveSpreadsheet(),
var sheets=ss.getSheets();
对于(变量i=0,iLen=sheets.length;i
2.部署Web应用程序。
  • 在脚本编辑器上,通过“发布”->“部署为web应用”打开对话框
  • 选择“我”以执行应用程序:“”。
    • 这样,脚本将作为所有者运行
  • 为有权访问应用程序的人选择“任何人”:。
    • 在这种情况下,需要访问令牌才能请求Web应用程序
  • 单击“部署”按钮作为新的“项目版本”
  • 自动打开“需要授权”对话框。
  • 单击“查看权限”
  • 选择自己的帐户
  • 单击“此应用未验证”处的“高级”
  • 单击“转到####项目名称###(不安全)”
  • 单击“允许”按钮
  • 单击“确定”
  • 3.测试此解决方法。 请单击分配有
    隐藏行
    显示行
    的按钮。这样,脚本由所有者运行。这样,即使用户单击按钮,脚本的结果也与所有者运行的结果相同

    注:
    • 请在启用V8时使用此脚本
    参考资料:
    • 相关问题

    在您的情况下,我认为此线程可能有用。在此线程中,为了以电子表格所有者的身份运行脚本,将使用Web应用程序。如果这没有用,我道歉。@Tanaike谢谢你,但没有用