简体   繁体   English

如何使用 Google App Script 将格式化文本从 google sheet 单元格保存到 .rtf?

[英]How to save formatted text from google sheet cells to .rtf with Google App Script?

谷歌电子表格中的标签“Newsticker”

I have a simple script in Google App Script, which takes string-values from cells in a google sheet (RSS-Feeds) and saves (updates) some.rtf - Documents.我在 Google App Script 中有一个简单的脚本,它从 google sheet(RSS-Feeds)中的单元格获取字符串值并保存(更新) some.rtf - Documents。 After some sync-processes and macro-helpers, those texts are displayed on a scrolling LED-bar, which can be updated through WIFI.经过一些同步过程和宏助手后,这些文本显示在滚动的 LED 条上,可以通过 WIFI 更新。

This works but I didn't manage to format the texts in the.rtf - Documents.这可行,但我没有设法格式化.rtf - 文档中的文本。

I experimented with the RichTextValueBuilder, which works fine to add formatting to text in a google spreadsheet but wasn't successful for formatting text in the.rtf's... Another experiment was with html-tags.我尝试了 RichTextValueBuilder,它可以很好地为谷歌电子表格中的文本添加格式,但在.rtf 中格式化文本没有成功......另一个实验是使用 html-tags。 This brought the desired formatting-effect in the rtf-file but the application for the led-bar couldn't handle it.这在 rtf 文件中带来了所需的格式化效果,但 led-bar 的应用程序无法处理它。

Does anybody know how to help a noob in this situation?有人知道在这种情况下如何帮助菜鸟吗? Thanks a lot in advance!提前非常感谢!

 function dataLED() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var ass = ss.getSheetByName("Newsticker"); //VALUES FROM CELLS IN GOOGLE SHEETS var valuesLedBar = ass.getRange(2, 10, 4, 1).getValues(); var vak1 = valuesLedBar[0]; var vak2 = valuesLedBar[1]; var vak3 = valuesLedBar[2]; var vak4 = valuesLedBar[3]; var marge = ass.getRange("B16").getValue(); //SAVE IN.RTF-FILES (IN FOLDER dataLEDbar) var folders = DriveApp.getFoldersByName("dataLEDbar"); if (folders.hasNext()) { var folder = folders.next(); saveData(folder, "0808003135589.rtf", vak1); saveData(folder, "0808003133343.rtf", vak2); saveData(folder, "0808003136619.rtf", vak3); saveData(folder, "0808003135964.rtf", vak4); saveData(folder, "0808182631849.rtf", "Gewinnmarge diesen Monat:" + marge); } else {null} }; //-------------------------------------------------SAVE function saveData(folder, fileName, contents) { var children = folder.getFilesByName(fileName); var file = null; if (children.hasNext()) { file = children.next(); file.setContent(contents); } else { file = folder.createFile(fileName, contents); } }

Here is my understanding:以下是我的理解:

  • You have a Google Spreadsheet which has the rich text in the cells of "J2:J5".您有一个 Google 电子表格,其中包含“J2:J5”单元格中的富文本。
  • You want to retrieve the rich texts from the cells of "J2:J5", and want to create them as each file with the mimeType of application/rtf .您想从“J2:J5”的单元格中检索富文本,并希望将它们创建为具有application/rtf的 mimeType 的每个文件。
    • In your case, 4 rich text files are created.在您的情况下,创建了 4 个富文本文件。
  • You want to achieve this using Google Apps Script.您想使用 Google Apps 脚本来实现这一点。

In this answer, in order to achieve your goal, I used a Google Apps Script library of RichTextApp .在这个答案中,为了实现您的目标,我使用了RichTextApp的 Google Apps 脚本库。

Usage:用法:

1. Install GAS library. 1. 安装 GAS 库。

Please install the GAS library of RichTextApp.请安装 RichTextApp 的 GAS 库。 You can see how to install at here .您可以在此处查看如何安装。

2. Sample script. 2. 示例脚本。

Please copy and paste the following sample script, and run myFunction .请复制并粘贴以下示例脚本,然后运行myFunction By this, when the folder is existing, 4 rich text files are created to the folder.这样,当文件夹存在时,会为该文件夹创建 4 个富文本文件。

function myFunction() {
  const filenames = ["0808003135589.rtf", "0808003133343.rtf", "0808003136619.rtf", "0808003135964.rtf"];
  const folderName = "dataLEDbar";
  const sheetName = "Newsticker";

  const folders = DriveApp.getFoldersByName(folderName);
  const folder = folders.hasNext() ? folders.next() : DriveApp.getRootFolder();
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName(sheetName);
  for (let i = 2; i <= 5; i++) {
    const range = sheet.getRange("J" + i);
    const doc = DocumentApp.create("tempDocument");
    const docId = doc.getId();
    RichTextApp.SpreadsheetToDocument({range: range, document: doc});
    doc.saveAndClose();
    const url = "https://docs.google.com/feeds/download/documents/export/Export?exportFormat=rtf&id=" + docId;
    const res = UrlFetchApp.fetch(url, {headers: {authorization: "Bearer " + ScriptApp.getOAuthToken()}});
    folder.createFile(res.getBlob().setName(filenames[i - 2]));
    DriveApp.getFileById(docId).setTrashed(true);
  }
}

Note:笔记:

  • This is a simple sample script.这是一个简单的示例脚本。 So please modify this for your actual situation.所以请根据您的实际情况进行修改。

Reference:参考:

Not sure, if this is helping anybody but I found a possible workaround for the problem.不确定,如果这对任何人都有帮助,但我找到了解决问题的可能方法。 Better solutions are welcome, as this looks ugly and will not work in every case...欢迎使用更好的解决方案,因为这看起来很难看,而且并非在所有情况下都有效......

The RTF-file is formatted beforehand and I just append the text from the google sheet instead of replacing the content and formatting of the RTF. RTF 文件是预先格式化的,我只是 append 来自谷歌表的文本,而不是替换 RTF 的内容和格式。

function appendText() {

//Search Params for File and Folder
var fileName = "testRtf.rtf";
var folderName = "dataLEDbar";
//New Text in .rtf
var content = "NEWTEXT";

// get list of folders with matching name
var folderList = DriveApp.getFoldersByName(folderName);
if (folderList.hasNext()) {
    //folder
    var folder = folderList.next();

    // search for files with matching name
    var fileList = folder.getFilesByName(fileName);

    if (fileList.hasNext()) {
        //file
        var file = fileList.next();

        //get rtf-file as string from Blob      
        var rtfContent = file.getBlob().getDataAsString();
        //split Blob 
        var splitBlob = rtfContent.split("\\");
        var lenSplitBlob = splitBlob.length;
        //and replace last array with new text
        splitBlob[lenSplitBlob - 1] = "cf2 " + content + "}";

        //new Blob
        var newBlob = splitBlob.toString().replace(/,/g, '\\');

        //append text to already formatted .rtf-File
        file.setContent(newBlob);

    } else {
        // file not found
        null
    }
}

} }

暂无
暂无

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

相关问题 Firebase | 谷歌表 | 应用脚本 | 如何将列中的数据而不是行中的数据从 Firebase 保存到 Google 表格? - Firebase | Google Sheet | App Script | How do I save the data in column instead of row from Firebase to Google Sheet? 使用Google脚本从Google表格中的单元格列表获取数组 - Using Google Script to get an array from a list of cells in google sheet Google App Script - 保存从我的工作表文件模板创建的工作表文件 ID - Google App Script - Save the sheet file Id created from my sheet file template 如何编辑 Google App 脚本以根据输入保存文件名并将其连接到 Google 工作表 - How to edit a Google App script to save the file name based on input and connect it to google sheet 如何将应用脚本中的换行符添加到 Google 表格 - How To Add Break Line From App Script To Google Sheet 如何从 Google Sheet App Script 返回 #N/A? - How to return #N/A from Google Sheet App Script? 如何在 App 脚本中循环 Google 表格公式 - How To Loop Google Sheet Formula In App Script 使用 App 脚本从 Google 表格添加或更新 Google 日历事件 - Add or Update Google Calendar Event from Google Sheet with App Script Google Spreadsheets 脚本 - 如何将范围从一张纸复制到另一张纸,但不覆盖非空目标单元格 - Google Spreadsheets script - How to copy range from one sheet to another sheet, but without overwriting non-empty target cells 当单元格被编辑为谷歌表格应用程序脚本中的某些文本时,如何触发 function? - How to trigger function when cell is edited to certain text in google sheet app script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM