[英]How to save a specific sheet of Google Spreadsheet a CSV values in a single cell of another sheet within the same Spreadsheet?
Can scripts be used to export the contents of one sheet as comma separated values, and save those comma separated values as text within a cell of a different sheet within the same spreadsheet?是否可以使用脚本将一张工作表的内容导出为逗号分隔值,并将这些逗号分隔值保存为同一电子表格中不同工作表单元格中的文本?
Conversely, can scripts be used to read the contents of the comma separated values saved within a single cell on one sheet and load/view them formatted on another sheet?相反,是否可以使用脚本读取保存在一张纸上的单个单元格中的逗号分隔值的内容,并在另一张纸上加载/查看它们的格式?
Context: I have a sheet (Sheet A) with various transactions separated by row.上下文:我有一张表(表 A),其中包含按行分隔的各种交易。 I'd like to use a second sheet (Sheet B) as a template to journal qualitative questions about these transactions.我想使用第二张表(表 B)作为模板来记录有关这些交易的定性问题。 At a click of a button or menu item, I'd like to save the contents of that 'journal entry' to a single cell within Sheet A. Additionally, I'd like a Sheet B, or a third sheet (C) to be able to select any given row of Sheet A and load the journal entry in a proper formatted way.单击按钮或菜单项,我想将该“日志条目”的内容保存到工作表 A 中的单个单元格中。此外,我希望工作表 B 或第三个工作表 (C)能够 select 表 A 的任何给定行并以正确格式的方式加载日记帐分录。 It'd be great if editing and viewing could be done on the same sheet (B) but not absolutely necessary如果可以在同一张纸上进行编辑和查看(B),那就太好了,但不是绝对必要的
Here is an example sheet of the data I'm working with.这是我正在使用的数据的示例表。 A solution with full code has been posted below.下面发布了带有完整代码的解决方案。
You can see in the 'Journal' sheet, buttons are created to:您可以在“日志”表中看到,创建按钮用于:
(1) Load Journal Template - This resets the information below Row 11, according to the 'Template' Sheet (1) 加载日记帐模板 - 这会根据“模板”表重置第 11 行下方的信息
(2) Load Journal entries - This loads the comma separated values (csv) from a single cell of the selected trade (2) 加载日志条目 - 这会从所选交易的单个单元格加载逗号分隔值 (csv)
(3) Save Journal entries - This saves the values within a subset of the below Journal area as a single cell of the trade selected by 'Select Trade' (3) 保存日志条目 - 这会将以下日志区域子集中的值保存为“选择交易”选择的交易的单个单元格
Would love to hear your thoughts!很想听听你的想法!
I think you don't need scripts for this action if this is small contents.如果这是小内容,我认为您不需要此操作的脚本。 When you speak about exporting sheet contents to a single cell I suppose you can use textjoin formula to put a table in a cell and separate it by comma.当您谈到将工作表内容导出到单个单元格时,我想您可以使用 textjoin 公式将表格放入单元格并用逗号分隔。
=textjoin(",";false;B1:C7)
- puts contents of B1:C7 in your cell. =textjoin(",";false;B1:C7)
- 将 B1:C7 的内容放入您的单元格中。
To unfold it, you can use SPLIT formula要展开它,您可以使用 SPLIT 公式
=split(B11;",")
makes a row of values from a content of B11从 B11 的内容生成一行值
= transpose(split(B11;","))
Makes it a coulmn.使它成为一个列。
If ranges are bigger you can still fold and unfold them without using a single script.如果范围更大,您仍然可以在不使用单个脚本的情况下折叠和展开它们。 But first I would habe to see some data structure.但首先我会看到一些数据结构。
This is the solution I managed to put together.这是我设法放在一起的解决方案。 Below is the code for each of these processes described beneath 'Edit' in the original question:以下是原始问题中“编辑”下描述的每个过程的代码:
(0) General Declarations (0) 一般声明
const ss = SpreadsheetApp.openById('###');
let log = ss.getSheetByName("Log");
let journal = ss.getSheetByName("Journal");
let logData = log.getDataRange().getValues();
let ticker = journal.getRange("A7").getValue();
let pnL = journal.getRange("H7").getValue();
let num = journal.getRange("B7").getValue();
let templates = ss.getSheetByName("Templates");
(1) Load Journal Template: (1) 加载日志模板:
function loadTemplates() {
// Clear current journal content and formatting
journal.getRange("12:100").clearContent();
journal.getRange("12:100").clearFormat();
journal.getRange("12:100").setDataValidation(null);
// Load Templates
// ABCD
if(journal.getRange("A3").getValue() == "ABCD"){
templates.getRange("2:61").copyTo(journal.getRange("12:71"));
// OTHER
} else if (ss.getSheetByName("Journal").getRange("A3").getValue() == "ORB"){
templates.getRange("63:122").copyTo(journal.getRange("12:71"));
};
};
(2) Load Journal (2) 加载日志
function loadJournal() {
for(let i = 0; i < logData.length; i++){
if(logData[i][5].toString().includes(ticker) && logData[i][22] === num){
journal.getRange("D13:G80").setValues(Utilities.parseCsv(logData[i][23]));
};
};
};
(3) Save Journal (3) 保存日志
function saveJournal() {
// Declaring the range that will make up the Array
let journalData = journal.getRange("D13:G80").getValues();
let output = [];
journalData.forEach(function(dataArray) {
let data = dataArray.join(",");
output += data + "\r\n";
});
// Finding correct trade to edit
for(let i = 0; i < logData.length; i++){
if(logData[i][5].toString().includes(ticker) && logData[i][22] === num){
log.getRange(i+1,24).setValue(output);
//Resize workaround using Google Sheets API bc setWrap and setRowHeight do not work
const requests = {updateDimensionProperties: {
properties: {pixelSize: 21},
range: {sheetId: log.getSheetId(), startIndex: i+1, endIndex: i+1, dimension: "ROWS"},
fields: "pixelSize"
}};
Sheets.Spreadsheets.batchUpdate({requests: requests}, ss.getId());
};
};
};
Would love to hear your thoughts!很想听听你的想法!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.