簡體   English   中英

使用帶有Google表格的Apps腳本:獲取簡短的網址並提取文件名

[英]Using Apps Script w/ Google Sheets: Get short URL & fetch file name

https://docs.google.com/spreadsheets/d/1sQyBTh7xCptnvr2SOQyEovmgIi6U-UapvDQOphk2Qxg/edit?usp=sharing

上面是我的Google表格文件,該文件鏈接到一個表單,該表單上載收據圖像文件並為上載的收據分類信息。

我對應用程序腳本工作有兩個要求:

    1) Get a short URL for the uploaded file. 
         STATUS: ACHIEVED!!!
    2) Fetch the name of the uploaded file for reference. 
         STATUS: NOT ACHIEVED, GET ERROR CODE BELOW WHEN CLICKING MENU ITEM
         TO RUN THE SCRIPT.

錯誤:“找不到具有給定ID的項目,或者您沒有訪問權限。”

我將稍后粘貼我的應用腳本代碼。

到目前為止,這是我所做的:

    1) Enabled both the Drive API & URL Shortener APIs in the script
    2) Enabled both APIs on Google API Console for this project
    3) Saved my code in Apps Script
    4) Ran onOpen() function to establish the Custom Menu in my spreadsheet 
       (it adds the menu when I refresh the spreadsheet).
    5) I am the owner of all of the following: 
            +the Drive account ALL of the pertinent files are stored on, 
            +the App Script that was created, 
            +the project in the Google API console.
    6) I have given consent to access the process when the Script first runs 
       with the same Google Account.

這是代碼:

function onOpen() {
  SpreadsheetApp.getUi()
  .createMenu("URL Manager")
  .addItem("Shorten","short")
  .addItem("Get File Names","names")
  .addToUi();
}

function short() {
  var range = SpreadsheetApp.getActiveRange(), data = range.getValues();
  var output1 = [];
  for(var i = 0, iLen = data.length; i < iLen; i++) {
    var url = UrlShortener.Url.insert({longUrl: data[i][0]});
    output1.push([url.id]);
  }
  //puts the short url one column to the right
  range.offset(0,1).setValues(output1);
}

function names() {
  var range = SpreadsheetApp.getActiveRange(), data = range.getValues();
  //var data must be converted to string since it is the long url of the 
  //file, then parsed for index of "=" (var n), then I sliced out the fileID 
  //with var m
  var str = data.toString();
  var n = str.indexOf("=");
  var m = str.slice(n+1)
  var output2 = [];
  for(var i = 0, iLen = data.length; i < iLen; i++) {
    var file = DriveApp.getFileById({fileID: m[i][0]});
    output2.push([file.getName()]);
  }
  //puts the file name 1 column to the left
  range.offset(0,-1).setValues(output2);
}

因此,我重新加載了電子表格,單擊以激活要為其獲取短url和文件名的單元格,然后單擊菜單項以執行腳本功能。 short()有效,names()無效。 看到上面的錯誤。

這是我的問題:

如何刪除文件訪問的隱藏限制?

我發現這個代碼(的OnOpen和短函數)的前2/3與一個偉大的解釋和說明這里 謝謝,亞歷克斯!

埃德·納爾遜Ed Nelson) ,您說得很對!

有問題的代碼行是這樣的:

var file = DriveApp.getFileById({fileID: m[i][0]});

這塊

    {fileID: m[i][0]}

在括號內返回[fileID],而DriveApp.getFileByID()實用程序需要輸入字符串(當包含'[]'時無法讀取)。

這是可行的解決方案:

 for (var i=0; i < data.length; i++) {
    var fileID = data[i][0].toString();
    var file = DriveApp.getFileById(fileID);

田田!

謝謝,埃德!

嘗試這個:

function names() {
  var range = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
  var rng=range.getDataRange()
  var data = rng.getValues();
  var output2 = [];
  for(var i = 1, iLen = data.length; i < iLen; i++) {
    var str = data[i][2].toString();
    var n = str.indexOf("=");
    var m = str.slice(n+1)
    var file = DriveApp.getFileById(m);
    output2.push([file.getName()]);
  }
  for(var j=0;j<output2.length;j++){
  range.getRange(j+2,2).setValue(output2[j][0]);
  }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM