[英]How to save formatted text from google sheet cells to .rtf with Google App Script?
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:以下是我的理解:
application/rtf
.application/rtf
的 mimeType 的每个文件。
In this answer, in order to achieve your goal, I used a Google Apps Script library of RichTextApp .在这个答案中,为了实现您的目标,我使用了RichTextApp的 Google Apps 脚本库。
Please install the GAS library of RichTextApp.请安装 RichTextApp 的 GAS 库。 You can see how to install at here .
您可以在此处查看如何安装。
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);
}
}
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.