javascript ScriptApp.newTrigger,所需权限

yzckvree  于 2023-02-02  发布在  Java
关注(0)|答案(1)|浏览(76)

我正在尝试创建一个触发器,以根据单元格值自动提取数据。我在工作表(或单元格)中创建并使用的自定义公式成功提取了数据,例如:
=列表插件(“https://example1.wpengine.com“)
这个函数看起来像这样:

function ListPlugins(url) {
  let sheet_name = 'apps and plugins'

  var options = {
     "async": true,
     "crossDomain": true,
     "method" : "GET",
     "headers" : {
       "Token" : TOKEN_HERE,
       "cache-control": "no-cache"
     }
   };
  var urlEncoded = encodeURI(url)
  let api_url = urlEncoded + "/wp-json/bd/v1/public/plugin-list/"
  var jsondata = UrlFetchApp.fetch(api_url,options)
  var object   = JSON.parse(jsondata.getContentText())

  return object;
}

但当我尝试使用触发器自动执行时,发生了错误

​Exception: You do not have permission to call ScriptApp.newTrigger. Required permissions: https://www.googleapis.com/auth/script.scriptapp

这就是我的自动化方法:

function onOpen(e) {
 return teste()
}

function teste()
{
  var all_data = getDataFromPluginList()

  /*return all_data 
  [ { first_cell: 'A2',
    last_cell: 'I9',
    first_row: 2,
    last_row: 9,
    total_row: 8,
    url: 'https://example1.wpengine.com/' },
  { first_cell: 'A10',
    last_cell: 'I15',
    first_row: 10,
    last_row: 15,
    total_row: 6,
    url: 'https://example2.wpengine.com/' } ]

  */

  for (var i = 0; i < all_data.length; i++) {
    trigger_(all_data[i].url);
  }
}

function trigger_(url)
{
 var new_trigger = ScriptApp.newTrigger(writePluginData).timeBased().everyMinutes(1).create();
 var trigger_id = new_trigger.getUniqueId();
 PropertiesService.getUserProperties().setProperty(trigger_id, url);
}

function writePluginData(event) 
{

  var trig = ScriptApp.getProjectTriggers();
  let sheet_name = 'trigger_result';
  let lowest_row = lowestRow(sheet_name);
  for(var i =0; i<trig.length; i++)
  {
   if(trig[i].getUniqueId()== event.triggerUid )
   {
    var urlParam = PropertiesService.getUserProperties().getProperty(event.triggerUid);
    urlParam = urlParam.split(",");
    var url = urlParam[0];
    var ss = SpreadsheetApp.getActive().getSheetByName(sheet_name); // change accordingly
    var data = ListPlugins(URL);

/*return listPlugins

[
    [
        "Escape Room",
        "https://example.com/",
        "Advanced Custom Fields PRO",
        "advanced-custom-fields-pro",
        "5.12.3",
        "6.0.7",
        false,
        6,
        "2023-01-30 10:57 AM"
    ],
    [
        "",
        "",
        "Classic Editor",
        "classic-editor",
        "1.6.2",
        "1.6.2",
        true,
        ""
    ],
    ...,
]
*/
    var target = "A"+ (lowest_row + 1)
    Logger.log(data);
    ss.getRange(target).setValue(data);
   }
  }
}

为什么会发生这个错误?2是否与我的API安全有关?

ajsxfq5m

ajsxfq5m1#

@tanaike第一个评论我的问题是我正在寻找的答案

相关问题