可以使用Google Apps Script编写代码来重新组织表格数据。

huangapple go评论114阅读模式
英文:

Is it possible to restructure table data with google-app script coding

问题

表格数据当前为:

标题 语言 份数
圣经 德语 7
白鲸 英语 6
麦克白 西班牙语 3
圣经 英语 6
动物庄园 俄语 6
动物庄园 乌克兰语 10

我需要在第二个表格中重新格式化,如下所示:

标题 德语 乌克兰语 英语 西班牙语 俄语
圣经 7 0 0 0 0
白鲸 0 0 6 0 0
麦克白 0 0 0 3 0
圣经 0 0 6 0 0
动物庄园 0 0 0 0 6
动物庄园 0 10 0 0 0

是否有应用脚本编码可以将表格重构为上述形式?

我正在尝试构建一个谷歌组合柱状图,所以我需要将语言作为列标题。这样我就可以将它们作为"系列"添加到谷歌图表功能中。

目前我只能想到手动解析数据的解决方案。

我尝试使用数据透视表来重新调整结构,但效果不佳。

英文:

Table data is currently

Titles Languages Copies
Bible German 7
Moby Dick English 6
MacBeth Spanish 3
Bible English 6
Animal Farm Russian 6
Animal Farm Ukrainian 10

I need to reformat the on a second sheet to look like this

Titles German Ukrain English Spanish Russian
Bible 7 0 0 0 0
Moby Dick 0 0 6 0 0
MacBeth 0 0 0 3 0
Bible 0 0 6 0 0
Animal Farm 0 0 0 0 6
Animal Farm 0 10 0 0 0

Is there app-script coding that can restructure the table to the above?

I am trying to build a Google Combo Bar Chart, so I need the languages as column headings. This way I can add them to the google charting feature as a "series".

At this point I can only think of a manual solution to parse out the data.

I tried using pivot tables to re-shape things, but it fell short.

答案1

得分: 0

我相信你的目标如下:

  • 你想将顶部表格转换为你问题中的底部表格。
  • 你想使用Google Apps Script来实现这个目标。
  • 在你展示的表格中,顶部表格使用了"Ukrainian"和"Ukrain",底部表格使用了"Ukrainian"。在这个答案中,请注意这一点。

以下是示例脚本:

示例脚本:

请将以下脚本复制粘贴到Google电子表格的脚本编辑器中,并在Google电子表格中设置源表和目标表的名称,然后保存脚本。

function myFunction() {
  const srcSheetName = "Sheet1"; // 请设置源表的名称。
  const dstSheetName = "Sheet2"; // 请设置目标表的名称。

  const dstHeader = ["Titles", "German", "Ukrainian", "English", "Spanish", "Russian"]; // 这是你问题中期望的结果。

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const srcSheet = ss.getSheetByName(srcSheetName);
  const dstSheet = ss.getSheetByName(dstSheetName);
  const [, ...srcValues] = srcSheet.getDataRange().getValues();
  const values = [dstHeader, ...srcValues.map(([a, b, c]) => {
    const temp = Array(dstHeader.length - 1).fill(0);
    const idx = dstHeader.indexOf(b);
    if (idx > -1) {
      temp.splice(idx - 1, 1, c);
    }
    return [a, ...temp];
  })];
  dstSheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}

测试:

运行此脚本时,将获得以下结果。除了你问题中"Ukrain"的拼写不同之外,这个结果似乎与你的底部表格相同。

可以使用Google Apps Script编写代码来重新组织表格数据。

注意:

  • 我只有你问题中的信息。因此,当你的实际情况与你展示的表格不同时,这个脚本可能无法使用。请注意这一点。

参考资料:

英文:

I believe your goal is as follows.

  • You want to convert the top table to the bottom table in your question.
  • You want to achieve this using Google Apps Script.
  • In your showing tables, "Ukrainian" and "Ukrain" are used in the top and bottom tables, respectively. In this answer, "Ukrainian" is used for both situations. Please be careful about this.

The sample script is as follows.

Sample script:

Please copy and paste the following script to the script editor of Google Spreadsheet and set your source and destination sheet names on the Google Spreadsheet, and save the script.

function myFunction() {
  const srcSheetName = "Sheet1"; // Please set the source sheet name.
  const dstSheetName = "Sheet2"; // Please set the destination sheet name.

  const dstHeader = ["Titles", "German", "Ukrainian", "English", "Spanish", "Russian"]; // This is from your expected result in your question.

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const srcSheet = ss.getSheetByName(srcSheetName);
  const dstSheet = ss.getSheetByName(dstSheetName);
  const [, ...srcValues] = srcSheet.getDataRange().getValues();
  const values = [dstHeader, ...srcValues.map(([a, b, c]) => {
    const temp = Array(dstHeader.length - 1).fill(0);
    const idx = dstHeader.indexOf(b);
    if (idx > -1) {
      temp.splice(idx - 1, 1, c);
    }
    return [a, ...temp];
  })];
  dstSheet.getRange(1, 1, values.length, values[0].length).setValues(values);
}

Testing:

When this script is run, the following result is obtained. It seems that this result is the same as your bottom table except for the spelling of "Ukrain" in your question.

可以使用Google Apps Script编写代码来重新组织表格数据。

Note:

  • I have only the information from your question. So, when your actual situation is different from your showing tables, this script might not be able to be used. Please be careful about this.

References:

答案2

得分: 0

<!-- language-all: js -->

只要你愿意尝试一种“纯公式解决方案”,而不是仅限于基于脚本的解决方案(出于某些原因);你可以尝试使用以下公式来重组数据并创建最终图表

=query({A2:C},"select Col1, sum(Col3) where Col1<>' ' group by Col1 pivot Col2")

可以使用Google Apps Script编写代码来重新组织表格数据。

替代公式:

=let(row,unique(tocol(A2:A,1)),
     col,unique(torow(B2:B,1),1),
     vstack(hstack("Titles",col),{row,map(row,lambda(Σ,bycol(col,lambda(Λ,let(Γ,sumifs(C:C,A:A,Σ,B:B,Λ),if(Γ>0,Γ,))))))}))
  • 这些公式是动态的(自动向下和向右扩展),当原始数据中的新标题/语言增加时,它们会自动适应列A:C中的数据
英文:

<!-- language-all: js -->

Just in case you are open to a plain formula solution and not exclusively tied to script-based solution(for X reasons); you could give this a shot to restructure data & create the end-chart

=query({A2:C},&quot;select Col1, sum(Col3) where Col1&lt;&gt;&#39;&#39; group by Col1 pivot Col2&quot;)

可以使用Google Apps Script编写代码来重新组织表格数据。

Alternate formula:

=let(row,unique(tocol(A2:A,1)),
     col,unique(torow(B2:B,1),1),
     vstack(hstack(&quot;Titles&quot;,col),{row,map(row,lambda(Σ,bycol(col,lambda(Λ,let(Γ,sumifs(C:C,A:A,Σ,B:B,Λ),if(Γ&gt;0,Γ,))))))}))
  • the formula(s) are dynamic(auto-expands downwards + right ways) as new titles /or languages add up in the raw data accommodated in columns A:C

huangapple
  • 本文由 发表于 2023年8月9日 13:33:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76864846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定