简体   繁体   English

Google Apps 脚本 - 有条件地将数据从其他 Google 表格检索到概览表格

[英]Google Apps Script - Conditionally retrieve data from other Google Sheet to Overview sheet

To explain the larger context: there are several forms which generate different sheets.为了解释更大的上下文:有几个 forms 生成不同的工作表。 I'm looking for a way to conditionally copy some of the responses sheet to a seperate "Overview" document.我正在寻找一种方法来有条件地将一些响应表复制到单独的“概述”文档中。 Code-wise, I had some ideas for the Overview document, but stranded near the start.在代码方面,我对 Overview 文档有一些想法,但在开始时就搁浅了。

My method was going to be to build functions for all the information I want to retrieve, such as date of birth (example in code block below), date of submission and phone number, when I click on a button.当我点击一个按钮时,我的方法是为我想要检索的所有信息构建函数,例如出生日期(下面代码块中的示例)、提交日期和电话号码。 The information may only be copied if the first and surname match the ones in the Overview.仅当名字和姓氏与概览中的匹配时,才能复制信息。 The order of the sheets in different docs are not the same and the column length is continually in flux.不同文档中的工作表顺序不一样,列长不断变化。 Furthermore, the amount of rows in the Overview doc is different than the form submission sheets.此外,Overview 文档中的行数与表单提交表不同。

In other words: if Anne Annenson would be the twenty-first respondent to a form, I want that information in the overview sheet where they are the first person.换句话说:如果 Anne Annenson 将成为表格的第 21 位受访者,我希望在概览表中他们是第一人称的信息。

function getDobs() {
    var targetSpreadsheet = SpreadsheetApp.getActive();
    var targetSheet = targetSpreadsheet.getSheetByName("Overview");
    var targetFirstNameCheck = targetSpreadsheet.getRange("A4:A");
    var targetSurnameCheck = targetSpreadsheet.getRange("B4:B"); 
    var sourceSpreadsheetDob = SpreadsheetApp.openById("..."); 
    var sourceDob = sourceSpreadsheetDob.getSheetByName("Form responses 1"); 
    var sourceFirstNameCheckDob = sourceSheetDob.getRange("C2:C"); 
    var sourceSurnameCheckDob = sourceSheetDob.getRange("D2:D"); 
    var sourceRangeDob = sourceSheetDobConsent.getRange("E2:E");

if (sourceFirstNameCheckDob==targetFirstNameCheck && sourceSurnameCheckDob==targetSurnameCheck){ //then I want to copy the data 
    var sourceData = sourceRangePronouns.getValues(); 
    var targetRangeDob = targetSheet.getRange("C4:C");  
 } 
    else (//I want it to leave the cells alone, so any text or formatting that might have been put in manually is still there.){ 
}
}

I would like for the responses to remain in the form response sheets as well.我希望回复也保留在表单回复表中。

Any thoughts?有什么想法吗?

Cooper already explained all the things you need in the comments. Cooper 已经在评论中解释了您需要的所有内容。 And below is what your code would look like following Cooper's comments.下面是您的代码在 Cooper 的评论之后的样子。

Code代码

function getDobs() {
  var targetSpreadsheet = SpreadsheetApp.getActive();
  var targetSheet = targetSpreadsheet.getSheetByName("Overview");
  var targetLastRow = targetSheet.getLastRow();
  // range equivalent to A4:B
  var targetNamesCheck = targetSheet.getRange(4, 1, targetLastRow - 3, 2).getValues();

  // tested in same spreadsheet, change "targetSpreadsheet" to openById on your actual script
  var sourceSpreadsheetDob = targetSpreadsheet; 
  var sourceDob = sourceSpreadsheetDob.getSheetByName("Form responses 1");
  var sourceLastRow = sourceDob.getLastRow();
  // range equivalent to C2:D
  var sourceNamesCheckDob = sourceDob.getRange(2, 3, sourceLastRow - 1, 2).getValues();
  // range for data to be copied (E2:G in my sample data)
  var sourceRangeDob = sourceDob.getRange(2, 5, sourceLastRow - 1, 3).getValues();

  var output = [];
  targetNamesCheck.forEach(function (targetNames) {
    // search sourceNamesCheckDob for targetNames 
    var index = searchForArray(sourceNamesCheckDob, targetNames);
    // if targetNames is in sourceNamesCheckDob, save the data on that row for later
    if (index > -1)
      output.push(sourceRangeDob[index]);
    // append blank cells if data is not found
    else
      output.push(new Array(sourceRangeDob[0].length)); 
  });
  // if there were names that were found, write the data beside the targetNames
  if (output.length > 0) {
    targetSheet.getRange(4, 3, output.length, output[0].length).setValues(output);
  }
}

// function to search the array for the object
function searchForArray(haystack, needle) {
  var i, j, current;
  for(i = 0; i < haystack.length; ++i) {
    if(needle.length === haystack[i].length) {
      current = haystack[i];
      for(j = 0; j < needle.length && needle[j] === current[j]; ++j);
      if(j === needle.length)
        return i;
    }
  }
  return -1;
}

Overview:概述:

概述

Form responses 1:表格回复1:

表单数据

Overview after running getDobs :运行getDobs后的概述:

输出

EDIT:编辑:

Since there are no methods that includes the apostrophe when the cell value is being fetched, easiest way is to have the sheets identify the phone number as text so it won't remove the 0 in the beginning.由于在获取单元格值时没有包含撇号的方法,因此最简单的方法是让工作表将电话号码标识为文本,这样它就不会在开头删除 0。 I've thought of 3 ways to have the 0 included in the output:我想了 3 种方法将 0 包含在 output 中:

  • Add the apostrophe manually on the specific output column via script通过脚本在特定的 output 列上手动添加撇号
  • Add dashes on the number so it is treated as text ( 09395398314 -> 093-9539-8314 ) (just an example, not sure if that is the right format)在数字上添加破折号,使其被视为文本( 09395398314 -> 093-9539-8314 )(只是一个例子,不确定这是否是正确的格式)
  • Format the output column into number -> plain text instead of number -> automatic将 output 列格式化为number -> plain text而不是number -> automatic

I prefer formatting the output column as that will be the fastest and easiest thing to do.我更喜欢格式化 output 列,因为这将是最快和最简单的事情。

Format:格式:

格式

Output: Output:

输出

Note:笔记:

  • This function will fill up rows where names in Overview are present in Form responses 1 .此 function 将填充表单响应 1中存在概览中名称的行。

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM