【问题标题】:Google Apps Script: Search for fields in 1 sheet, copy to location in different sheetGoogle Apps 脚本:在 1 个工作表中搜索字段,复制到不同工作表中的位置
【发布时间】:2017-01-18 00:48:07
【问题描述】:

我是 google 应用程序脚本的新手,当我搜索某些东西时,我得到的答案与我正在寻找的不同(例如,如果我 google:应用程序脚本搜索表,我会得到有关添加搜索框的说明电子表格)。

我正在尝试制作一个脚本,它将成绩从包含学生测验成绩(由谷歌表单测验生成)的自动生成的电子表格复制到主评分表(这是 2 个不同的文档)。我有点困惑,因为我看到的大多数关于复制信息的示例似乎都不是在两个不同的文档之间复制,而是从一个文档中的两个不同的工作表(选项卡)复制。每个学生都有一个登录名,例如 john smith 可能是 jsmith01。电子表格中的每一行对应一位学生。有一堆列,但我正在尝试使用“登录”列和“测验”列。登录列包含学生登录(例如 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
}

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    您需要按 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();
    }
    

    【讨论】:

      【解决方案2】:

      刚开始时,我习惯将源数据保存在数组中,然后将数据迭代到目标工作表中。

      然后我找到了复制方法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);
      

      【讨论】:

      • 嗨,这似乎不是在搜索学生证。我不只是想将一列从一张纸复制到另一张纸上。我需要将一张表中的学生 ID 匹配到下一张表中,并将源表中每个 ID 的相应成绩复制到目标表中与同一学生 ID 对应的单元格中。学生的顺序可能不一定相同,或者源表中可能缺少某些学生。谢谢!
      【解决方案3】:

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

      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)
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-10
        • 1970-01-01
        相关资源
        最近更新 更多