无法从Google电子表格运行独立的Google App脚本

时间:2017-10-02 14:09:39

标签: google-apps-script google-sheets

我有与同事共享的Google电子表格,以跟踪任务状态。我想通过菜单设置一个功能来阻止任务完成的某些范围。保护权限设置为仅允许我(所有者)编辑它。

我已使用run as me为此共享电子表格为UrlFetch构建了一个独立脚本。我尝试了很多种代码,但它没有用。以下是我试过的3个版本。

当然,我设置了#34;谁可以访问该应用:任何人,甚至匿名"在独立脚本中。 Photo 1: Standalone script setting

非常感谢!

第一版:不使用内容服务

//In standalone script
function doGet(e){
   var taskRange = e.parameter.taskRange;
   var ss = SpreadsheetApp.openById('SHARING_SPREADSHEET_ID');
   var sh = ss.getSheetByName('Sheet1');
   var protection = sh.getRange(taskRange).protect().setDescription('Completed Task')
   var result = protection.removeEditors(protection.getEditors())
   return result
   }

//In sharing spreadsheet script
var url = "https://script.google.com/macros/s/STANDALONE_SCRIPT_ID/exec"
function onOpen(){
SpreadsheetApp.getUi().createMenu('TASK')
  .addItem('Protect Task', 'protectTask')
  .addToUi();
}
function protectTask(){
var taskRange = SpreadsheetApp.getActiveSheet().getActiveRange()
var response = UrlFetchApp.fetch(url+"?taskRange="+taskRange);
Logger.log(response);
}

第二版:使用内容服务

//In standalone script
function doGet(e){
   var taskRange = e.parameter.taskRange;
   var ss = SpreadsheetApp.openById('SHARING_SPREADSHEET_ID');
   var sh = ss.getSheetByName('Sheet1');
   var protection = sh.getRange(taskRange).protect().setDescription('Completed Task')
   var result = protection.removeEditors(protection.getEditors())
   return ContentService.createTextOutput(result)
   }

//In sharing spreadsheet script
var url = "https://script.google.com/macros/s/STANDALONE_SCRIPT_ID/exec"
function onOpen(){
SpreadsheetApp.getUi().createMenu('TASK')
  .addItem('Protect Task', 'protectTask')
  .addToUi();
}
function protectTask(){
var taskRange = SpreadsheetApp.getActiveSheet().getActiveRange()
var response = UrlFetchApp.fetch(url+"?taskRange="+taskRange).getContentText();
Logger.log(response);
}

第3版:使用带有JSON的内容服务

//In standalone script
function doGet(e){
   var taskRange = e.parameter.taskRange;
   var ss = SpreadsheetApp.openById('SHARING_SPREADSHEET_ID');
   var sh = ss.getSheetByName('Sheet1');
   var protection = sh.getRange(taskRange).protect().setDescription('Completed Task')
   var result = protection.removeEditors(protection.getEditors())
   return ContentService.createTextOutput(JSON.stringify(result)).setMimeType(ContentService.MimeType.JSON)
   }

//In sharing spreadsheet script
var url = "https://script.google.com/macros/s/STANDALONE_SCRIPT_ID/exec"
function onOpen(){
SpreadsheetApp.getUi().createMenu('TASK')
  .addItem('Protect Task', 'protectTask')
  .addToUi();
}
function protectTask(){
var taskRange = SpreadsheetApp.getActiveSheet().getActiveRange()
var response = UrlFetchApp.fetch(url+"?taskRange="+taskRange).getContentText();
var result = JSON.Parse(response)
Logger.log(result);
}

2 个答案:

答案 0 :(得分:0)

这一行存在一个问题:

var taskRange = SpreadsheetApp.getActiveSheet().getActiveRange()

应该是:

var taskRange = SpreadsheetApp.getActiveSheet().getActiveRange().getA1Notation();

目前,变量taskRange是一个“类”它不是一个字符串。你需要发一个字符串。

答案 1 :(得分:0)

最终的工作。我认为脚本运行不正常,因为我使用多种代码修改了很多次。因此,我创建了一个新的并使用了" stringified"登录@SandyGood建议。 非常感谢大家,特别是@SandyGood。

最终脚本

//In standalone script
function doGet(e){
var taskRange = e.parameter.taskRange;
var ss = SpreadsheetApp.openById('SHARING_SPREADSHEET_ID');
var sh = ss.getSheetByName('Sheet1');
var protection = sh.getRange(taskRange).protect().setDescription('Completed Task');
var result = protection.removeEditors(protection.getEditors());
var resultStr = JSON.stringify(result);
Logger.log('resultStr: ' + resultStr);
}

//In sharing spreadsheet script
var url = "https://script.google.com/macros/s/STANDALONE_SCRIPT_ID/exec"
function onOpen(){
SpreadsheetApp.getUi().createMenu('TASK')
   .addItem('Protect Task', 'protectTask')
   .addToUi();
}
function protectTask(){
var taskRange = SpreadsheetApp.getActiveSheet().getActiveRange().getA1Notation();
var response = UrlFetchApp.fetch(url+"?taskRange="+taskRange).getContentText();
Logger.log(response);
}