[英]How to transpose and split in Google Apps Script?
I am using 2 sheets, rawData
and processedData
. 我使用2张,
rawData
和processedData
。
rawData
looks like this: rawData
看起来像这样:
Title
Options
Title
Options
Title1
Option1, Option2, Option3, Option4
Title1
Option1, Option2, Option3, Option4
Title2
Option1, Option2, Option3, Option4, Option5
Title2
Option1, Option2, Option3, Option4, Option5
Title2
Option1, Option2, Option3, Option4, Option5
Title3
Option1, Option2, Option3
Title3
Option1, Option2, Option3
processedData
should look like this: processedData
应如下所示:
Title
Options
Title
Options
Title1
Option1
Title1
Option1
Title1
Option2
Title1
Option2
Title1
Option3
Title1
Option3
Title1
Option4
Title1
Option4
Title2
Option1
Title2
Option1
Title2
Option2
Title2
Option2
Title2
Option3
Title2
Option3
Title2
Option4
Title2
Option4
Title2
Option5
Title2
Option5
Title3
Option1
Title3
Option1
Title3
Option2
Title3
Option2
Title3
Option3
Title3
Option3
In Google Sheets, I would use the formula =TRANSPOSE(SPLIT(rawData!B2,",",TRUE,TRUE))
to get the options for the first title in the format I need. 在Google表格中,我将使用公式
=TRANSPOSE(SPLIT(rawData!B2,",",TRUE,TRUE))
获得所需格式的第一个标题的选项。
I'm trying to create a Google Apps Script that could take each row of rawData
and convert it to the format shown in processedData
. 我正在尝试创建一个Google Apps脚本,该脚本可以使用
rawData
每一行并将其转换为processedData
显示的格式。
function formatData() {
// File
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Get rawData sheet
var rawData = ss.getSheetByName('rawData');
// Get processedData sheet
var processedData = ss.getSheetByName('processedData');
// Get range that contains titles, get its values
var titlesRange = rawData.getRange('A2:A');
var titlesRangeValues = titlesRange.getValues();
// Get range that contains options, get its values
var optionsRange = rawData.getRange('B2:B');
var optionsRangeValues = optionsRange.getValues();
// Get number of rows in rawData sheet
var titlesCount = GSheetsUtils.getRowsData(rawData);
// Get last row in processedData sheet
var lastRow = processedData.getLastRow();
// Copy each title to processedData and format the options
for (var i = 0; i<titlesCount.length; i++) {
processedData.getRange(lastRow + 1,1).setValue(titlesRangeValues[i][0]);
processedData.getRange(lastRow + 1,2).setValue(optionsRangeValues[i][0]);
lastRow++;
}
}
Executing this code will populate processedData
like so: 执行此代码将以如下方式填充
processedData
:
Title
Options
Title
Options
Title1
Option1, Option2, Option3, Option4
Title1
Option1, Option2, Option3, Option4
Title2
Option1, Option2, Option3, Option4, Option5
Title2
Option1, Option2, Option3, Option4, Option5
Title2
Option1, Option2, Option3, Option4, Option5
Title3
Option1, Option2, Option3
Title3
Option1, Option2, Option3
How can I get the script to split and transpose the options but write the same title for each option? 如何获得脚本来拆分和转置选项,但为每个选项写相同的标题?
Thank you in advance. 先感谢您。
PS: The code is likely not efficient and I assume it lacks any best practices, it's my first time using Google Apps Script so if anyone has any pointers I would appreciate it. PS:该代码可能效率不高,我认为它缺乏最佳实践,这是我第一次使用Google Apps脚本,因此,如果有人有任何指针,我将不胜感激。 I have been looking through Google's documentation but I need to sleep on it.
我一直在浏览Google的文档,但是我需要睡一会儿。
One way to solve this is to 解决此问题的一种方法是
Best practices suggest minimising calls to the sheet. 最佳做法建议尽量减少对工作表的呼叫。
So the following variation of your script does all of the above: 因此,脚本的以下变体可以完成上述所有操作:
function formatData() {
// File
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Get rawData sheet
var rawData = ss.getSheetByName('rawData');
// Input data
var data = rawData.getRange("A2:B").getValues(); // Gets titles and options in a single call to the sheet
// Initialise an array that will hold the output
var outputArray = [];
// Name a variable to hold the data from each set of options
var options;
// Start looping through the data
for (var row = 0; row < data.length; row++) {
// Split the options into an array: "Option1, Option2, Option3" ---> [Option1, Option2, Option3]
options = data[row][1].split(", ");
// Loop through the array of split options and place each of them in a new row
for (var element = 0; element < options.length; element++) {
outputArray.push([data[row][0], // Place the title in a new row
options[element]]); // Place one option in the 2nd column of the row
} // Options loop ends here
} // Data loop ends here
// Get processedData sheet
var processedData = ss.getSheetByName('processedData');
// Get last row in processedData sheet
var lastRow = processedData.getLastRow();
// Post the outputArray to the sheet in a single call
processedData.getRange(lastRow + 1, 1, outputArray.length, outputArray[0].length).setValues(outputArray);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.