Ausgabe
Ich automatisiere meine Google Sheets in App-Skripten. Ich bin auf ein Szenario gestoßen, in dem eine Zelle mehrere Werte hat. Ich möchte explode
es wie in Python.
Beispiel Ein- und Ausgabe:
input = [["Hi",12,"One line
two line
three line", "God"],["Bye",13,"check","man"]]
output = [["Hi",12,"One line","God"],["Hi",12,"two line","God"],["Hi",12,"Three line","God"],["Bye",13,"check","man"]]
Nachfolgend mein Teilversuch
var final = y.map(r => {
return r.map(x => {
if(x.toString().indexOf("/n") > -1)
{
x = x.toString().split("/n");
}
return x;
});
});
Lösung
Verwenden Sie reduce
zum Schleifen: split
die Zeichenfolge durch neue Zeile \n
, map
das geteilte Array und verbinden Sie es mit der übergeordneten Zeile und dem vorherigen Akkumulator und flat
.
/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/
const input = [["Hi",12,"One line\ntwo line\nthree line", "God"],["Bye",13,"check","man"]];
const output = input.reduce((acc,[a,b,c],i) => [acc,c.split("\n").map(sc => [a,b,sc])].flat() ,[]);
console.log(output);
<!-- https://meta.stackoverflow.com/a/375985/ --> <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
Beantwortet von – TheMaster
Antwort geprüft von – Mildred Charles (FixError Admin)