可以使用Google Apps Script代码重新组织表格数据吗?

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

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

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

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

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

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

英文:

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电子表格的脚本编辑器中,并设置您的源表格和目标表格的名称,然后保存脚本。

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

=查询({A2:C},"选择 Col1,sum(Col3) where Col1<>'' group by Col1 pivot Col2")

替代公式:

=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-2.html
匿名

发表评论

匿名网友

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

确定