簡體   English   中英

谷歌驅動器文件鏈接自動更改電子表格中的格式

[英]google drive file link automatically change format in spreadsheet

我正在通過谷歌應用程序腳本在谷歌表格中存儲驅動器鏈接,在谷歌驅動器中實現一些下載和上傳文件功能。 上傳工作正常,但一段時間后鏈接變成某種超鏈接,如下所示

圖像

所以這就是為什么我不再能夠簡單地獲取鏈接 writting.getDisplayValue() const ss = SpreadSheetApp.getActive().getActiveSheet() let url = ss.getRange().getDisplayValue()

任何建議...?

我嘗試了 Adding.getRichTextValue().getLinkUrl() 但它也不起作用,因為它不是超鏈接

問題和解決方法:

根據您的示例圖像,在您的情況下,文件的鏈接已更改為智能芯片。 不幸的是,在當前階段,還沒有在電子表格上管理智能芯片的方法。 因此,在這種情況下,需要使用變通方法。 解決方法如下。

  1. 將 Google 電子表格轉換為 XLSX 數據。
    • 至此,智能芯片的文件鏈接被轉換為簡單的字符串和超鏈接。
  2. 將 XLSX 數據轉換為 Google 電子表格。
  3. 從單元格中檢索超鏈接。

此方法來自How to get in Apps Script the value of a dropdown in a Google Doc? https://tanaikech.github.io/2022/10/27/retrieving-values-of-calendar-events-of-smart-chips-on-google-document-using-google-apps-script/

當此流程反映在示例腳本中時,以下示例腳本如何?

示例腳本:

請將以下腳本復制並粘貼到 Google Spreadsheet 的腳本編輯器中,並將要檢索超鏈接的range設置為 A1Notation。 在此示例中,使用驅動器 API。 因此, 請在 Advanced Google services 啟用 Drive API

function myFunction() {
  const range = "Sheet1!A1:A10"; // Please set the range you want to retrieve the hyperlinks.

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const url = "https://docs.google.com/spreadsheets/export?exportFormat=xlsx&id=" + ss.getId();
  const blob = UrlFetchApp.fetch(url, { headers: { authorization: "Bearer " + ScriptApp.getOAuthToken() } }).getBlob();
  const tempId = Drive.Files.insert({ mimeType: MimeType.GOOGLE_SHEETS, title: "temp" }, blob).id;
  const tempFile = DriveApp.getFileById(tempId);
  const tempSS = SpreadsheetApp.open(tempFile);
  const res = tempSS.getRange(range).getRichTextValues().map((r, i) => r.map((c, j) => ({ value: c.getText(), url: c.getLinkUrl() || "", range: { row: i + 1, column: j + 1 } })));
  tempFile.setTrashed(true);
  console.log(res);
}

測試:

運行此腳本時,將獲得以下結果。

[
  [{"value":"sample value","url":"https://drive.google.com/file/d/###/view?usp=share_link","range":{"row":1,"column":1}}],
  ,
  ,
  ,
]

筆記:

  • 作為另一種方法,在你展示的示例圖片中,如果你想將智能芯片的文件鏈接轉換為帶有超鏈接的正常值,下面的示例腳本怎么樣? 在此示例中, range被具有從 XLSX 數據轉換獲得的超鏈接的正常值覆蓋。

     function myFunction2() { const range = "Sheet1:A1;A10". // Please set the range you want to retrieve the hyperlinks. const ss = SpreadsheetApp;getActiveSpreadsheet(): const url = "https.//docs.google?com/spreadsheets/export.exportFormat=xlsx&id=" + ss;getId(). const blob = UrlFetchApp,fetch(url: { headers: { authorization. "Bearer " + ScriptApp.getOAuthToken() } });getBlob(). const tempId = Drive.Files:insert({ mimeType. MimeType,GOOGLE_SHEETS: title, "temp" }. blob);id. const tempFile = DriveApp;getFileById(tempId). const tempSS = SpreadsheetApp;open(tempFile). const r = tempSS;getRange(range). const tempSheet = r.getSheet();copyTo(ss). tempSheet.getRange(r.getA1Notation()).copyTo(ss;getRange(range)). ss;deleteSheet(tempSheet). tempFile;setTrashed(true); }

參考:

暫無
暫無

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

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