我正在使用2张纸,rawData
和processedData
。rawData
看起来像这样:Status
Title
Options
Live
Title1
Option1, Option2, Option3, Option4
Live
Title2
Option1, Option2, Option3, Option4, Option5
Live
Title3
Option1, Option2, Option3
processedData
应该看起来像这样:Status
Title
Options
Live
Title1
Option1
empty
Title1
Option2
empty
Title1
Option3
empty
Title1
Option4
Live
Title2
Option1
empty
Title2
Option2
empty
Title2
Option3
empty
Title2
Option4
empty
Title2
Option5
Live
Title3
Option1
empty
Title3
Option2
empty
Title3
Option3
empty
应该为空,不知道如何将其保留为空白以表示一个空单元格。
我尝试使用此代码,但无法通过循环获得所需的输出。
function formatData() {
// File
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Get rawData sheet
var rawData = ss.getSheetByName('rawData');
// Input data
var data = rawData.getRange("A2:C").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;
var status;
// Start looping through the data
for (var row = 0; row < data.length; row++) {
status = data[row][0];
outputArray.push([status]);
// 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);
}
我是该语言的初学者,很抱歉提出了一个非常基本的问题。
最佳答案
这个修改怎么样?请认为这只是几种修改之一。
模式1:
修改后的脚本:
修改脚本后,它如下所示。请按如下所示修改for循环。
for (var row = 0; row < data.length; row++) {
status = data[row][0];
var title = data[row][1]; // Added
// outputArray.push([status]); // Removed
options = data[row][2].split(", "); // Modified
for (var element = 0; element < options.length; element++) {
if (element == 0) { // Added and modified
outputArray.push([status, title, options[element]]);
} else {
outputArray.push(["", title, options[element]]);
}
}
}
Pattetn 2:
作为另一种模式,下面的修改如何?使用此功能时,请进行以下修改。
修改后的脚本:
从:
// Initialise an array that will hold the output
var outputArray = [];
// Name a variable to hold the data from each set of options
var options;
var status;
// Start looping through the data
for (var row = 0; row < data.length; row++) {
status = data[row][0];
outputArray.push([status]);
// 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
至:
var outputArray = data.reduce(function(ar1, e) {
var h1 = e.splice(0, 1)[0];
var h2 = e.splice(0, 1)[0];
var options = e[0].split(", ");
return ar1.concat(options.reduce(function(ar2, f, j) {
if (f) ar2.push(j == 0 ? [h1, h2, f] : ["", h2, f]);
return ar2;
}, []));
}, []);
结果与模式1相同。
注意:
在您的脚本中,似乎没有复制“状态,标题,选项”的标题。这个怎么样?
参考:
reduce()
如果我误解了您的问题,而这不是您想要的结果,我表示歉意。