简体   繁体   English

谷歌驱动器文件链接自动更改电子表格中的格式

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

I am implementing some download and upload file functionality in google drive through google app-script storing the drive link in google sheet.我正在通过谷歌应用程序脚本在谷歌表格中存储驱动器链接,在谷歌驱动器中实现一些下载和上传文件功能。 upload works fine but after some time the link is turn into some kind of hyperlink as like below上传工作正常,但一段时间后链接变成某种超链接,如下所示

图像

so that's why I am no longer able to get the link simply writting.getDisplayValue() const ss = SpreadSheetApp.getActive().getActiveSheet() let url = ss.getRange().getDisplayValue()所以这就是为什么我不再能够简单地获取链接 writting.getDisplayValue() const ss = SpreadSheetApp.getActive().getActiveSheet() let url = ss.getRange().getDisplayValue()

Any Suggestion...?任何建议...?

I tried Adding.getRichTextValue().getLinkUrl() But It also does not worked as It is not a HyperLink我尝试了 Adding.getRichTextValue().getLinkUrl() 但它也不起作用,因为它不是超链接

Issue and workaround:问题和解决方法:

From your sample image, in your situation, the link of the file is changed to the smart chip.根据您的示例图像,在您的情况下,文件的链接已更改为智能芯片。 In the current stage, unfortunately, there are no methods for managing the smart chips on a Spreadsheet.不幸的是,在当前阶段,还没有在电子表格上管理智能芯片的方法。 So, in this case, it is required to use a workaround.因此,在这种情况下,需要使用变通方法。 The workaround is as follows.解决方法如下。

  1. Convert Google Spreadsheet to XLSX data.将 Google 电子表格转换为 XLSX 数据。
    • By this, the file links of the smart chip are converted to simple strings and hyperlinks.至此,智能芯片的文件链接被转换为简单的字符串和超链接。
  2. Convert XLSX data to Google Spreadsheet.将 XLSX 数据转换为 Google 电子表格。
  3. Retrieve the hyperlinks from the cells.从单元格中检索超链接。

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

When this flow is reflected in a sample script, how about the following sample script?当此流程反映在示例脚本中时,以下示例脚本如何?

Sample script:示例脚本:

Please copy and paste the following script to the script editor of Google Spreadsheet and set range that you want to retrieve the hyperlinks as A1Notation.请将以下脚本复制并粘贴到 Google Spreadsheet 的脚本编辑器中,并将要检索超链接的range设置为 A1Notation。 In this sample, Drive API is used.在此示例中,使用驱动器 API。 So, please enable Drive API at Advanced Google services .因此, 请在 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);
}

Testing:测试:

When this script is run, the following result is obtained.运行此脚本时,将获得以下结果。

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

Note:笔记:

  • As another approach, in your showing sample image, if you want to convert the file links of the smart chip to the normal value with the hyperlink, how about the following sample script?作为另一种方法,在你展示的示例图片中,如果你想将智能芯片的文件链接转换为带有超链接的正常值,下面的示例脚本怎么样? In this sample, range is overwritten by the normal values with the hyperlinks obtained by converting from XLSX data.在此示例中, 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); }

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM