久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何通過自定義函數(shù)/腳本從具有正確授權(quán)的 Go

How to get the file URL from file name in Google Sheets with correct Authorization via custom function/script(如何通過自定義函數(shù)/腳本從具有正確授權(quán)的 Google 表格中的文件名中獲取文件 URL) - IT屋-程序員軟件開
本文介紹了如何通過自定義函數(shù)/腳本從具有正確授權(quán)的 Google 表格中的文件名中獲取文件 URL的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我想創(chuàng)建一個(gè)自定義函數(shù),從 Google 表格中的文件名中提取驅(qū)動(dòng)器 URL.

I would like to create a custom function that pulls a Drive URL from a file name in Google Sheets.

所以,使用下面的代碼:

So, using the code below:

  1. 如果我在單元格 A1
  2. 中有一個(gè)有效的文件名
  3. 函數(shù) =getFile(A1) 將返回 URL

  • 當(dāng)我在腳本編輯器中運(yùn)行我的腳本時(shí),返回值有效.
  • 當(dāng)我在工作表中運(yùn)行函數(shù) getFile() 時(shí),出現(xiàn)以下錯(cuò)誤.
  • When I run my script from within the script editor, the return value works.
  • When I run the function getFile() from within my sheet, I get the error below.

我的代碼:

function getFile(cell) {

  var filename = encodeURI(cell);

  var url = "https://www.googleapis.com/drive/v3/files?fields=files(id,name)&q=name+contains+'" + filename + "' and trashed=false";
  var params = {
    method: "GET",
    headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
    muteHttpExceptions: true
  };
  var res = UrlFetchApp.fetch(url, params).getContentText();
  var json = JSON.parse(res);
  return res; // outputs response below
  if(json){
    var objFiles = json.files[0];
    var fileID = objFiles.id
    var resURL = "https://docs.google.com/spreadsheets/d/" + fileID;
    Logger.log(resURL);
    //return resURL; // only works when run within script editor
  }

}

錯(cuò)誤:

"{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "authError",
    "message": "Invalid Credentials",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Invalid Credentials"
 }
}
"

我猜我的 Auth 令牌有問題.有人可以指導(dǎo)我解決這個(gè)問題嗎?提前致謝!

I'm guessing something's wrong with my Auth token. Can someone direct me to resolving this? Thanks in advance!

推薦答案

@I'-'I的answer 是正確的.雖然我不確定這是否是您想要的,但這個(gè)解決方法怎么樣?我也遇到過同樣的問題.當(dāng)時(shí),我使用了以下解決方法.

@I'-'I's answer is correct. Although I'm not sure whether this is what you want, how about this workaround? I have also experienced the same issue. At that time, I had used the following workaround.

  • 使用 PropertiesService 設(shè)置和獲取訪問令牌.

流程如下.

  1. 由時(shí)間驅(qū)動(dòng)的觸發(fā)器每 1 小時(shí)設(shè)置一次訪問令牌.
    • 由此,訪問令牌每 1 小時(shí)更新一次.因?yàn)閍ccess token的過期時(shí)間是1小時(shí).
  • 這樣就可以使用訪問令牌了.

修改腳本:

請(qǐng)將此功能安裝為時(shí)間驅(qū)動(dòng)觸發(fā)器.當(dāng)然,你也可以手動(dòng)運(yùn)行這個(gè)函數(shù).

Modified script:

Please install this function as the time-driven trigger. Of course, you can run manually this function.

function setAccessToken() {
  PropertiesService.getScriptProperties().setProperty("accessToken", ScriptApp.getOAuthToken());
}

在你的腳本中,請(qǐng)修改如下.

In your script, please modify as follows.

var params = {
  method: "GET",
  headers: {"Authorization": "Bearer " + ScriptApp.getOAuthToken()},
  muteHttpExceptions: true
};

到:

var params = {
  method: "GET",
  headers: {"Authorization": "Bearer " + PropertiesService.getScriptProperties().getProperty("accessToken")},
  muteHttpExceptions: true
};

注意:

  • 在這種情況下,訪問令牌的所有者是項(xiàng)目的所有者.
  • 我認(rèn)為CacheService也可以使用這個(gè).
    • PropertiesService

    這篇關(guān)于如何通過自定義函數(shù)/腳本從具有正確授權(quán)的 Google 表格中的文件名中獲取文件 URL的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

    【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機(jī)器人提及發(fā)出該機(jī)器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復(fù)必須使用導(dǎo)入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務(wù)器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復(fù)“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務(wù)器時(shí)的歡迎消息)
主站蜘蛛池模板: 国产日韩精品一区二区三区 | 激情综合五月 | 免费在线看黄视频 | 99精品一区二区三区 | 亚洲欧美日韩久久久 | 国产高清视频一区二区 | 精品久久99 | 秋霞av国产精品一区 | 久久99精品久久久久久国产越南 | 91麻豆精品一区二区三区 | 中文字幕不卡在线观看 | 超碰欧美| 2019天天操| 黄色在线免费看 | 欧美国产激情 | 在线视频中文字幕 | 97av在线| 国产精品一区二区视频 | 亚洲免费精品 | 97伦理| 久久国产精品视频观看 | 亚洲九九色 | 美女逼网站 | 狠狠骚 | 日韩中文字幕视频 | 精品一区在线免费观看 | 日日干日日 | 国产成人网 | 自拍偷拍亚洲欧美 | 久久国产精品视频观看 | 日韩一及片| 爱综合 | 久久精品综合 | 韩日一区 | 黄色av网站在线免费观看 | 一区二区免费在线视频 | 国产精品久久国产愉拍 | 国产精品美女久久久免费 | 久久成人国产精品 | 成人欧美一区二区三区1314 | 国产精品免费一区二区三区 |