Google Apps脚本:搜索1张中的字段,复制到不同表格

时间:2016-09-09 19:52:08

标签: google-apps-script google-sheets

我是谷歌应用脚​​本的新手,当我搜索一些东西时,我得到的答案不同于我寻找的答案(例如,如果我谷歌:应用脚本搜索表,我会收到添加说明电子表格的搜索框。)

我试图创建一个脚本,将自动生成的电子表格中的成绩从包含学生测验成绩(由Google表单测验生成)复制到主评分表(这些是2个不同的文档)。我有点困惑,因为我看到的关于复制信息的大多数示例似乎都不是在两个不同的文档之间复制,而是从一个文档中的两个不同的工作表(选项卡)复制。每个学生都有一个登录名,例如john smith可能是jsmith01。电子表格中的每一行对应一名学生。有很多专栏,但我尝试使用"登录"专栏和"测验"柱。登录栏保存学生登录(ex jsmith01),测验栏包含测验成绩。这是我尝试做的一些伪代码:

quizGrades = spreadsheet with results automatically created by quiz form
masterGrades = spreadsheet that I need to copy quiz grades into

for curStudent in rows on quizGrades sheet{
    studentLogin = login name of curStudent in login column in quizGrades sheet
    studentGrade = value (quiz grade) for curStudent in quiz column in quizGrades sheet
    currStudentRow = row where login col. value in masterGrades sheet matches studentLogin

    copy studentGrade into grade col. in currentStudentRow in masterGrades sheet
}

3 个答案:

答案 0 :(得分:0)

您需要按ID

打开其中一个电子表格
function moveAnswersToOtherSpreadsheet() {
  var answers,masterGradesSS,quizGradesSS,sheetTabWithAnswers;//Declare variables without assigning a value

  masterGradesSS = SpreadsheetApp.getActiveSpreadsheet();//spreadsheet with results automatically created by quiz form
  quizGradesSS = SpreadsheetApp.openById("Put the ID here"); //spreadsheet to copy quiz grades into

  sheetTabWithAnswers = masterGradesSS.getSheetByName("name here");

  //getRange(start row, start column, number of Rows to get, number of Columns to get)
  answers = sheetTabWithAnswers.getRange(1, 1, sheetTabWithAnswers.getLastRow(), sheetTabWithAnswers.getLastColumn()).getValues();
}

答案 1 :(得分:0)

当我开始时,我曾经将数据保存在数组中,然后将数据迭代到目标表中。

然后我找到了复制方法in the API documentation。希望这有用:

 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var source = ss.getSheets()[0];

 var destination = SpreadsheetApp.openById("abc1234567");
 var destinationSheet = destination.getSheets()[1];

 var range = source.getRange("B2:D4");

 // This copies the data in B2:D4 in the source sheet to
 // D4:F6 in the second spreadSheet
 range.copyValuesToRange(destinationSheet, 4, 6, 4, 6);

答案 2 :(得分:0)

也许这很有帮助。我做了三个功能(我以前用过)。它们返回电子表格中搜索的术语的行和列坐标。

function searchForStudentId(SPREADSHEET_ID, SHEET_NAME, studentId) {
    var locatedCells = [];
    var ss = SpreadsheetApp.openById(SPREADSHEET_ID);
    var searchLocation = ss.getSheetByName(SHEET_NAME).getDataRange().getValues();        
    //Loops to find the search term. 
        for (var j = 0, jLen = searchLocation.length; j < jLen; j++) {
            for (var k = 0, kLen = searchLocation.length; k < kLen; k++) {
                var find = studentId;
                if (find == searchLocation[j][k]) {
                    locatedCells.push({ 'found': (j + 1) + "," + (k + 1)});
                }
            }
        }
     //   Logger.log(locatedCells);
        return(locatedCells)
    }


 function LocateStudentIdsInSource (){
  var SPREADSHEET_ID = "ADD ID"
  var SHEET_NAME = "Sheet1"
  var studentId = "1234567"
  var studentIdSourceLocation = searchForStudentId(SPREADSHEET_ID,    SHEET_NAME, studentId)
  Logger.log(studentIdSourceLocation);
}


  function LocateStudentIdsInTarget (){
  var SPREADSHEET_ID = "ADD ID"
  var SHEET_NAME = "Sheet1"
  var studentId = "1234567"
  var studentIdTargetLocation = searchForStudentId(SPREADSHEET_ID, SHEET_NAME, studentId)
  Logger.log(studentIdTargetLocation)
  }