英文:
Google Apps Script to export svg data in cell to png file
问题
我有一个已经设置好的 Google 表格,它可以基于其他表格中的数值生成 SVG 数据 - 我经常用它来制作记分卡、排行榜等。我已经设置好了这样一个功能,其中一个单元格(以本示例而言,假设是"Sheet1!A1")包含了一个 SVG 文件的所有代码行。我目前有一个脚本,可以将这个单元格中的代码导出为 SVG 文件。
然而,之后我还需要额外的步骤来将这些 SVG 转换成 PNG,以便在社交媒体等地方分享。是否有人知道如何将 Google 表格中单元格中的 SVG 代码导出为 PNG 文件的方法?我知道这可能只是我的一厢情愿,但如果能实现这个功能将会非常棒。
英文:
I have a google sheet that is set up to generate svg data based on values in other sheets - I use this often for scorecards, leader boards etc. I have this set up so that one cell (for the sake of this example let's say it is "Sheet1!A1") contains all the lines of code for an svg file. I currently have a script which takes this cell and exports it as an SVG.
However, I then have the additional step of converting these svgs into pngs so I can share them on social media etc. Does anyone know of a way to take the svg code in a cell in google sheets and export this as a PNG file? It's definitely wishful thinking on my part, but it would be amazing if this functionality was possible
答案1
得分: 1
以下是翻译好的内容,代码部分未翻译:
我相信您的目标如下。
- 在您的情况下,SVG 代码放在 Google 电子表格的单元格 "A1" 中。
- 您希望从单元格 "A1" 中检索 SVG 代码并将其转换为 PNG 图像。
- 您希望使用 Google Apps 脚本来实现这一目标。
在这种情况下,以下示例脚本如何?
示例脚本:
请将以下脚本复制并粘贴到电子表格的脚本编辑器中,并将您的单元格设置为 A1 记法的 range
。还需要 在高级 Google 服务中启用 Drive API。
function myFunction() {
const range = "Sheet1!A1"; // 请将您的单元格设置为 A1 记法。
const svgCode = SpreadsheetApp.getActiveSpreadsheet().getRange(range).getDisplayValue().trim();
const temp = DriveApp.createFile("sample.svg", svgCode, MimeType.SVG);
Utilities.sleep(3000); // 可能不需要使用这一行。
const url = Drive.Files.get(temp.getId()).thumbnailLink.replace("=s220", "=s1000");
const blob = UrlFetchApp.fetch(url).getBlob();
DriveApp.createFile(blob.setName("sample.png"));
// temp.setTrashed(true); // 如果要删除 SVG 文件,请使用这一行。
}
-
当运行此脚本
myFunction()
时,SVG 代码将从 "Sheet1!A1" 的单元格中检索,并创建一个 SVG 文件。然后,使用 Drive API 将从 SVG 文件中检索 PNG 图像。并且将 PNG 文件创建为 PNG 文件。 -
在此示例中,PNG 文件将被创建在根文件夹中。如果要将 PNG 文件导出到特定文件夹,请将
DriveApp.createFile(blob.setName("sample.png"))
修改为DriveApp.getFolderById("###folderID###").createFile(blob.setName("sample.png"))
。
注意:
- 不幸的是,我无法了解您的 "SVG 代码"。因此,如果无法使用此脚本,请提供您的示例 "SVG 代码"。这样,我可以进行确认。
参考:
英文:
I believe your goal is as follows.
- In your situation, the SVG code is put into a cell "A1" on Google Spreadsheet.
- You want to retrieve the SVG code from the cell "A1" and convert it as a PNG image.
- You want to achieve this using Google Apps Script.
In this case, how about the following sample script?
Sample script:
Please copy and paste the following script to the script editor of Spreadsheet and please set your cell as A1Notation to range
. And, please enable Drive API at Advanced Google services.
function myFunction() {
const range = "Sheet1!A1"; // Please set your cell as A1Notation.
const svgCode = SpreadsheetApp.getActiveSpreadsheet().getRange(range).getDisplayValue().trim();
const temp = DriveApp.createFile("sample.svg", svgCode, MimeType.SVG);
Utilities.sleep(3000); // This might not be required to be used.
const url = Drive.Files.get(temp.getId()).thumbnailLink.replace("=s220", "=s1000");
const blob = UrlFetchApp.fetch(url).getBlob();
DriveApp.createFile(blob.setName("sample.png"));
// temp.setTrashed(true); // If you want to delete the SVG file, please use this line.
}
-
When this script
myFunction()
is run, the SVG code is retrieved from a cell of "Sheet1!A1", and an SVG file is created. And, a PNG image is retrieved from the SVG file by converting using Drive API. And, the PNG file is created as a PNG file. -
In this sample, the PNG file is created in the root folder. If you want to export the PNG file to the specific folder, please modify
DriveApp.createFile(blob.setName("sample.png"))
toDriveApp.getFolderById("###folderID###").createFile(blob.setName("sample.png"))
.
Note:
- Unfortunately, I cannot know your
the svg code
. So, when this script cannot be used, please provide your samplethe svg code
. By this, I would like to confirm it.
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论