Google Apps脚本:暂时以其他用户身份运行脚本 [英] Google Apps Script: Temporarily run script as another user

查看:102
本文介绍了Google Apps脚本:暂时以其他用户身份运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Google Spreadsheet,其中包含一些表单,每个表单或我使用除了特定单元格例程保护共享用户以避免编辑他们不应该的区域。



我也有一个插入行的函数,但由于保护作用,当它作为共享用户运行时,显然会失败。

插入行功能有效100%,没有添加任何保护。



如何在脚本运行时临时将活动用户更改为'我',然后在离开工作表时将'删除我'在其原始状态包括所有的保护?

解决方案

最可行的选择是有一个Web应用程序插入所需的行。您可以通过get或post方法传递表名和行号,下面的方法使用get方法
您的google脚本如下所示:

 函数insertRow(){
var ss = SpreadsheetAp p.getActive()
var sheetName = ss.getActiveSheet()。getName()
var row = ss.getActiveRange()。getRow()
// URL下面是链接到web应用程序,您将通过发布>部署为网络应用程序来生成它。下面是Web应用程序的代码。
var url =把生成的URL放在这里
var queryString =?sheetName =+ sheetName +& rowNo =+ row $ b $ url url = url + queryString
Logger .log(url)
var request = UrlFetchApp.fetch(url)$ b $ if(request!='Success')
Browser.msgBox(Sorry Unable to insertRow)
}

接下来,您将部署一个Web应用程序,它将处理获取请求并将新行添加到片。代码如下所示:

 函数doGet(e){
var SheetID =您的工作表ID在这里
var param = e.queryString
var parameters = param.split(&)
//这只是检查只有2个参数存在否则给出无效链接
if (param!= null&& parameter.length == 2){
param = e.parameter
var name = param.sheetName
var row = Number(param.rowNo)
} else {
return ContentService.createTextOutput(Failed)
}
try {
var ss = SpreadsheetApp.openById(sheetID)
var sheet = ss.getSheetByName(name)
sheet.insertRowAfter(row)
sheet.getRange(row + 1,1).setValue(Inserted Row)
}
catch(err ){
return ContentService.createTextOutput(Failed)
}
return ContentService.createTextOutput(Success)
}

确保运行webApp一次以授予它授权/许可来编辑您的谷歌表格。
由于webApp将在您的授权下运行,因此它将能够在没有许可权的情况下插入Row。另外,您还可以向获取请求添加唯一ID(有效用户的电子邮件ID),以确保只处理来自Google表格脚本内的有效请求。



如果您想了解更多关于网络应用程序的详细信息,请点击此处: https://developers.google.com/apps-script/guides/web


I have a Google Spreadsheet containing a number of sheets, each or which I have protected using the "Except certain cells' routine in order to protect shared users from editing areas they shouldn't.

I also have a function to insert rows but this obviously fails when running as a shared user due to the protection.

The Insert Rows function works 100% with no protection added.

How can I temporarily change the active user to 'me' while the script is running and then 'remove me' when completed leaving the sheet in its original state including all the protection?

解决方案

The most viable option is to have a web App to insert the row where desired. You can pass the sheet Name and row No via a get or post method. The below method uses a get method. Your google script would look something like this:

function insertRow() {
  var ss = SpreadsheetApp.getActive()
  var sheetName = ss.getActiveSheet().getName()
  var row = ss.getActiveRange().getRow()
  // Below URL is the link to  web App, you will generate it by publish >Deploy as web app. Code for Web app is below. 
  var url ="Put the generated URL here"
  var queryString = "?sheetName="+sheetName+"&rowNo="+row
  url = url + queryString
  Logger.log(url)
  var request = UrlFetchApp.fetch(url)
  if (request != 'Success')
    Browser.msgBox("Sorry Unable to insertRow")
}

Next, you will deploy a web App which will process the get request and add new rows to the sheet. The code would look like this:

function doGet(e) {
  var SheetID = "Your Sheet ID here"
  var param = e.queryString
  var parameters = param.split("&")
  // This just checks only 2 parameters are present else gives a invalid link
  if (param != null && parameters.length == 2){
    param = e.parameter
    var name = param.sheetName
    var row = Number(param.rowNo)
    } else {
    return ContentService.createTextOutput("Failed")
    }
  try{  
     var ss = SpreadsheetApp.openById(sheetID)  
     var sheet = ss.getSheetByName(name)
     sheet.insertRowAfter(row)
     sheet.getRange(row + 1,1).setValue("Inserted Row")
     }
     catch (err){
      return ContentService.createTextOutput("Failed")
     }
 return ContentService.createTextOutput("Success")
}

Make sure to run the webApp once to give it authorization/permission to edit your google sheets. Since the webApp will run under your authorization, it would be able to insert Row without an issue of permission. Also, you can add a unique ID(Email ID of the effective User?) to the get request to make sure only valid request from within the google sheets scripts is processed.

If you would like more details on the web app you can find it here: https://developers.google.com/apps-script/guides/web

这篇关于Google Apps脚本:暂时以其他用户身份运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆