我想写一些编码来从不同的Google工作表中提取数据,并将它们带入一个主工作表中。我已经编写了编码(附加)。它可以工作,但是确实很混乱和可怕。自从我编码以来已有很多年了,不记得该如何整理了。我在想某种for循环,但我如此生锈,想知道是否有人可以提供帮助?谢谢。
正如我所说,我想从许多不同的表中提取数据。工作表包含来自各种Google表单的数据,我发现这是将数据整合到一个工作表中的唯一方法。这是一种非常讨厌的方法,但是它可以工作。
function pulldata() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
// Source
var sourcesheet = ss.getSheetByName("Converting Fractions, Decimals and Percentages (1)");
var sourcesheet1 = ss.getSheetByName("Converting Fractions, Decimals and Percentages (2)");
var sourcesheet2 = ss.getSheetByName("Recall Mixed Division Facts");
var sourcesheet3 = ss.getSheetByName("Recall 4x Table");
var sourcesheet4 = ss.getSheetByName("Recall 3x Table");
var sourcesheet5 = ss.getSheetByName("Recall Addition and Subtraction Facts within 20");
var sourcesheet6 = ss.getSheetByName("Recall 6x Table");
var sourcesheet7 = ss.getSheetByName("Recall 7x Table");
var sourcesheet8 = ss.getSheetByName("Recall 8x Table Facts");
var sourcesheet9 = ss.getSheetByName("Recall 9x Table");
var sourcesheet10 = ss.getSheetByName("8x Table Division Facts");
var sourcesheet11 = ss.getSheetByName("Addition Facts within 20");
var sourcesheet12 = ss.getSheetByName("Recall Divisibility Rules for 2, 5 and 10");
var sourcesheet13 = ss.getSheetByName("Recall Divisibility Rules for 3 and 9");
var sourcesheet14 = ss.getSheetByName("3x Table Division Facts");
var sourcesheet15 = ss.getSheetByName("4x Table Division Facts");
var sourcesheet16 = ss.getSheetByName("6x Table Division Facts");
var sourcesheet17 = ss.getSheetByName("7x Table Division Facts");
var sourcesheet18 = ss.getSheetByName("Recall Mixed Tables Facts");
var sourcesheet19 = ss.getSheetByName("9x Table Division Facts");
var sourcesheet20 = ss.getSheetByName("Recall Multiples of 100 that add to 1000");
var sourcesheet21 = ss.getSheetByName("Recall Subtraction Facts within 20");
var sourcesheet22 = ss.getSheetByName("Recall five and facts");
var sourcesheet23 = ss.getSheetByName("Use Mixed Table Facts to Solve Bigger Problems");
var sourcesheet24 = ss.getSheetByName("Recall 2x Table");
var sourcesheet25 = ss.getSheetByName("Recall Addition and Subtraction Facts within 14");
var sourcesheet26 = ss.getSheetByName("Recall 5x Table");
var sourcesheet27 = ss.getSheetByName("Ten AND facts, with corresponding subtraction");
var sourcesheet28 = ss.getSheetByName("Recall 10x Table");
var sourcesheet29 = ss.getSheetByName("Recall adding facts to 10");
var sourcesheet30 = ss.getSheetByName("Recall addition facts within 10");
var sourcesheet31 = ss.getSheetByName("Division for 2x Tables");
var sourcesheet32 = ss.getSheetByName("Recall Division Facts for 5 times table");
var sourcesheet33 = ss.getSheetByName("Division Facts for 10x Table");
var sourcesheet34 = ss.getSheetByName("Recall doubles to 20");
var sourcesheet35 = ss.getSheetByName("Recall Subtraction Facts from 20");
var sourcesheet36 = ss.getSheetByName("Recall Multiples of 10 that add to 100");
var sourcesheet37 = ss.getSheetByName("Recall Subtraction Facts to 10");
var sourcesheet38 = ss.getSheetByName("Subtraction within 10");
var sourcesheet39 = ss.getSheetByName("Halves to 20");
var sourcesheet40 = ss.getSheetByName("Recall 5 AND subtraction within 10");
// Target
var targetsheet = ss.getSheetByName("All Responses");
// Set source ranges
var sourcerange = sourcesheet.getSheetValues(2,1,sourcesheet.getLastRow(),21);
var sourcerange1 = sourcesheet1.getSheetValues(2,1,sourcesheet1.getLastRow(),21);
var sourcerange2 = sourcesheet2.getSheetValues(2,1,sourcesheet2.getLastRow(),21);
var sourcerange3 = sourcesheet3.getSheetValues(2,1,sourcesheet3.getLastRow(),21);
var sourcerange4 = sourcesheet4.getSheetValues(2,1,sourcesheet4.getLastRow(),21);
var sourcerange5 = sourcesheet5.getSheetValues(2,1,sourcesheet5.getLastRow(),21);
var sourcerange6 = sourcesheet6.getSheetValues(2,1,sourcesheet6.getLastRow(),21);
var sourcerange7 = sourcesheet7.getSheetValues(2,1,sourcesheet7.getLastRow(),21);
var sourcerange8 = sourcesheet8.getSheetValues(2,1,sourcesheet8.getLastRow(),21);
var sourcerange9 = sourcesheet9.getSheetValues(2,1,sourcesheet9.getLastRow(),21);
var sourcerange10 = sourcesheet10.getSheetValues(2,1,sourcesheet10.getLastRow(),21);
var sourcerange11 = sourcesheet11.getSheetValues(2,1,sourcesheet11.getLastRow(),21);
var sourcerange12 = sourcesheet12.getSheetValues(2,1,sourcesheet12.getLastRow(),21);
var sourcerange13 = sourcesheet13.getSheetValues(2,1,sourcesheet13.getLastRow(),21);
var sourcerange14 = sourcesheet14.getSheetValues(2,1,sourcesheet14.getLastRow(),21);
var sourcerange15 = sourcesheet15.getSheetValues(2,1,sourcesheet15.getLastRow(),21);
var sourcerange16 = sourcesheet16.getSheetValues(2,1,sourcesheet16.getLastRow(),21);
var sourcerange17 = sourcesheet17.getSheetValues(2,1,sourcesheet17.getLastRow(),21);
var sourcerange18 = sourcesheet18.getSheetValues(2,1,sourcesheet18.getLastRow(),21);
var sourcerange19 = sourcesheet19.getSheetValues(2,1,sourcesheet19.getLastRow(),21);
var sourcerange20 = sourcesheet20.getSheetValues(2,1,sourcesheet20.getLastRow(),21);
var sourcerange21 = sourcesheet21.getSheetValues(2,1,sourcesheet21.getLastRow(),21);
var sourcerange22 = sourcesheet22.getSheetValues(2,1,sourcesheet22.getLastRow(),21);
var sourcerange23 = sourcesheet23.getSheetValues(2,1,sourcesheet23.getLastRow(),21);
var sourcerange24 = sourcesheet24.getSheetValues(2,1,sourcesheet24.getLastRow(),21);
var sourcerange25 = sourcesheet25.getSheetValues(2,1,sourcesheet25.getLastRow(),21);
var sourcerange26 = sourcesheet26.getSheetValues(2,1,sourcesheet26.getLastRow(),21);
var sourcerange27 = sourcesheet27.getSheetValues(2,1,sourcesheet27.getLastRow(),21);
var sourcerange28 = sourcesheet28.getSheetValues(2,1,sourcesheet28.getLastRow(),21);
var sourcerange29 = sourcesheet29.getSheetValues(2,1,sourcesheet29.getLastRow(),21);
var sourcerange30 = sourcesheet30.getSheetValues(2,1,sourcesheet30.getLastRow(),21);
var sourcerange31 = sourcesheet31.getSheetValues(2,1,sourcesheet31.getLastRow(),21);
var sourcerange32 = sourcesheet32.getSheetValues(2,1,sourcesheet32.getLastRow(),21);
var sourcerange33 = sourcesheet33.getSheetValues(2,1,sourcesheet33.getLastRow(),21);
var sourcerange34 = sourcesheet34.getSheetValues(2,1,sourcesheet34.getLastRow(),21);
var sourcerange35 = sourcesheet35.getSheetValues(2,1,sourcesheet35.getLastRow(),21);
var sourcerange36 = sourcesheet36.getSheetValues(2,1,sourcesheet36.getLastRow(),21);
var sourcerange37 = sourcesheet37.getSheetValues(2,1,sourcesheet37.getLastRow(),21);
var sourcerange38 = sourcesheet38.getSheetValues(2,1,sourcesheet38.getLastRow(),21);
var sourcerange39 = sourcesheet39.getSheetValues(2,1,sourcesheet39.getLastRow(),21);
var sourcerange40 = sourcesheet40.getSheetValues(2,1,sourcesheet40.getLastRow(),21);
var thestring = "A1:Z";
var thelastrow = targetsheet.getLastRow();
var range = thestring+thelastrow;
if (thelastrow != 0) {
targetsheet.getRange(range).clearContent();
}
// Write values
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange.length,21).setValues(sourcerange);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange1.length,21).setValues(sourcerange1);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange2.length,21).setValues(sourcerange2);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange3.length,21).setValues(sourcerange3);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange4.length,21).setValues(sourcerange4);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange5.length,21).setValues(sourcerange5);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange6.length,21).setValues(sourcerange6);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange7.length,21).setValues(sourcerange7);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange8.length,21).setValues(sourcerange8);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange9.length,21).setValues(sourcerange9);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange10.length,21).setValues(sourcerange10);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange11.length,21).setValues(sourcerange11);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange12.length,21).setValues(sourcerange12);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange13.length,21).setValues(sourcerange13);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange14.length,21).setValues(sourcerange14);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange15.length,21).setValues(sourcerange15);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange16.length,21).setValues(sourcerange16);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange17.length,21).setValues(sourcerange17);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange18.length,21).setValues(sourcerange18);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange19.length,21).setValues(sourcerange19);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange20.length,21).setValues(sourcerange20);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange21.length,21).setValues(sourcerange21);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange22.length,21).setValues(sourcerange22);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange23.length,21).setValues(sourcerange23);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange24.length,21).setValues(sourcerange24);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange25.length,21).setValues(sourcerange25);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange26.length,21).setValues(sourcerange26);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange27.length,21).setValues(sourcerange27);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange28.length,21).setValues(sourcerange28);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange29.length,21).setValues(sourcerange29);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange30.length,21).setValues(sourcerange30);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange31.length,21).setValues(sourcerange31);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange32.length,21).setValues(sourcerange32);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange33.length,21).setValues(sourcerange33);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange34.length,21).setValues(sourcerange34);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange35.length,21).setValues(sourcerange35);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange36.length,21).setValues(sourcerange36);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange37.length,21).setValues(sourcerange37);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange38.length,21).setValues(sourcerange38);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange39.length,21).setValues(sourcerange39);
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourcerange40.length,21).setValues(sourcerange40);
}
我希望我可以使用更简洁的代码来做同样的事情。
答案 0 :(得分:0)
使用工作表名称的数组,它们将在它们之间进行映射和逐个查找。
const sheetNames = ["Converting Fractions, Decimals and Percentages (1)",
"Converting Fractions, Decimals and Percentages (2)",
"Recall Mixed Division Facts",
"Recall 4x Table",
"Recall 3x Table",
"Recall Addition and Subtraction Facts within 20"]; // I am not typing them all in
const sourceSheets = sheetNames.map(name => ss.getSheetByName(name));
const targetsheet = ss.getSheetByName("All Responses");
const sourceRanges = sourceSheets.map(sourceSheet => sourceSheet.getSheetValues(2,1,sourcesheet.getLastRow(),21));
var thestring = "A1:Z";
var thelastrow = targetsheet.getLastRow();
var range = thestring+thelastrow;
if (thelastrow != 0) {
targetsheet.getRange(range).clearContent();
}
sourceRanges.forEach(sourceRange => {
targetsheet.getRange(targetsheet.getLastRow()+1, 1, sourceRange.length,21).setValues(sourceRange);
});
答案 1 :(得分:0)
尝试一下:
function pullData1() {
var inclA=["Converting Fractions, Decimals and Percentages (1)", "Converting Fractions, Decimals and Percentages (2)", "Recall Mixed Division Facts", "Recall 4x Table", "Recall 3x Table", "Recall Addition and Subtraction Facts within 20", "Recall 6x Table", "Recall 7x Table", "Recall 8x Table Facts", "Recall 9x Table", "8x Table Division Facts", "Addition Facts within 20", "Recall Divisibility Rules for 2, 5 and 10", "Recall Divisibility Rules for 3 and 9", "3x Table Division Facts", "4x Table Division Facts", "6x Table Division Facts", "7x Table Division Facts", "Recall Mixed Tables Facts", "9x Table Division Facts", "Recall Multiples of 100 that add to 1000", "Recall Subtraction Facts within 20", "Recall five and facts", "Use Mixed Table Facts to Solve Bigger Problems", "Recall 2x Table", "Recall Addition and Subtraction Facts within 14", "Recall 5x Table", "Ten AND facts, with corresponding subtraction", "Recall 10x Table", "Recall adding facts to 10", "Recall addition facts within 10", "Division for 2x Tables", "Recall Division Facts for 5 times table", "Division Facts for 10x Table", "Recall doubles to 20", "Recall Subtraction Facts from 20", "Recall Multiples of 10 that add to 100", "Recall Subtraction Facts to 10", "Subtraction within 10", "Halves to 20","Recall 5 AND subtraction within 10"];//I did not type them I copied them from your source and modified with UltraEdit and regular expressions
var ss=SpreadsheetApp.getActive();
var tarsh=ss.getSheetByName("All Responses");
tarsh.clearContents();
for(var i=0;i<inclA.length;i++){
var srcsh=ss.getSheetByName(inclA[i]);
var srcvA=srcsh.getSheetValues(2,1,srcsh.getLastRow(),21);
tarsh.getRange(tarsh.getLastRow()+1,1,srcvA.length,21).setValues(srcvA);
}
}
我没有对此进行测试。