简体   繁体   English

如何有效地将数据从一个 Google 表格行中的多个单元格传输到另一个具有不同单元格排列的 Google 表格上的模板

[英]How to efficiently transfer data from multiple cells in one Google Sheet row to a template on another google sheet with different cell arrangement

I have a google sheet (named "Template") which I use to capture client data.我有一个用于捕获客户数据的谷歌表格(名为“模板”)。 Data entry cells in this "Template" sheet are not arranged in a sequential manner due to the way that the template is designed.由于模板的设计方式,此“模板”工作表中的数据输入单元格未按顺序排列。 I have a code to save this data to a second google sheet serving as my database( named "Data").我有一个代码可以将这些数据保存到作为我的数据库(名为“数据”)的第二个谷歌表格中。 I am writing a code to search the saved client records from "Data" by client unique identifier (client ID).我正在编写代码以通过客户端唯一标识符(客户端 ID)从“数据”中搜索已保存的客户端记录。 After running the client record search function, I expect the data to be populated back to the original "Template".运行客户端记录搜索 function 后,我希望数据被填充回原来的“模板”。 I can't seem to find an efficient way of achieving this using google apps script.我似乎找不到使用 google apps 脚本实现此目的的有效方法 The "Data" sheet will be having up to 50 columns of data per client.每个客户端的“数据”表最多包含 50 列数据。

Below is the code that I wrote.下面是我写的代码。 The code works as expected but I feel it's a long way of doing it and there might be a better and shorter way of achieving this when dealing with upwards of 50 columns per client.该代码按预期工作,但我觉得这样做很长,而且在每个客户端处理超过 50 列时,可能有更好和更短的方法来实现这一点。 Is there a way to copy the entire row in "Data" sheet and paste the respective values to the range/array (["D3", "B1", "B2", "E2", B4",...] as in example below) in "Template" sheet using few lines of code, as opposed to setting value for each and every one of the 50 cells? Am still new to coding.有没有办法复制“数据”表中的整行并将相应的值粘贴到范围/数组(["D3", "B1", "B2", "E2", B4",...] as在下面的示例中)在“模板”工作表中使用几行代码,而不是为 50 个单元格中的每个单元格设置值?我对编码还是陌生的。

var ss = SpreadsheetApp.getActiveSpreadsheet();
templateS = ss.getSheetByName("Template");
dataS = ss.getSheetByName("Data");

//function to retrieve client record and populate template
function searchRecord() 

var searchCell = templateS.getRange("B6"); //cell holding the search value
var searchValue = searchCell.getValue(); // value to be used to search. Unique client ID. 


// Search record in data sheet using unique client ID
var recordFound = dataS.getRange("A:A") //client ID in column A of "Data"
                   .createTextFinder(searchValue) 
                   .matchCase(true)
                   .matchEntireCell(true)
                   .findNext();

var row = recordFound.getRow(); //capture row position containing the search value. 

//**populate template with up to 50 lines of below code**

templateS.getRange("D3").setValue(dataS.getRange(row, 1).getValue()); //capture client ID
templateS.getRange("B1").setValue(dataS.getRange(row, 2).getValue()); //capture title
templateS.getRange("B2").setValue(dataS.getRange(row, 3).getValue()); //capture surname
templateS.getRange("E2").setValue(dataS.getRange(row, 4).getValue()); //capture first name
templateS.getRange("B4").setValue(dataS.getRange(row, 5).getValue()); //capture address
}

I believe your goal is as follows.我相信你的目标如下。

  • Your showing script works fine.您的显示脚本工作正常。
  • You want to reduce the process cost of your showing script.您希望降低放映脚本的处理成本。

In this case, how about using Sheets API?在这种情况下,使用 Sheets API 怎么样? When Sheets API is used, the process cost can be reduced a little.使用Sheets API时,加工成本可略有降低。 Ref When Sheets API is used to your script, how about the following modification? Ref当 Sheets API 用于您的脚本时,如何进行以下修改?

Modified script:修改脚本:

Before you use this script, please enable Sheets API at Advanced Google services .在使用此脚本之前, 请在 Advanced Google services 中启用 Sheets API

In this sample, I used var dstRnages = ["D3", "B1", "B2", "E2", "B4"];在此示例中,我使用var dstRnages = ["D3", "B1", "B2", "E2", "B4"]; as the destination ranges.作为目的地范围。 This is from your showing script.这是来自您的展示脚本。 If you want to add more ranges, please modify it like var dstRnages = ["D3", "B1", "B2", "E2", "B4", "##", "##",,,,];如果要添加更多范围,请修改为var dstRnages = ["D3", "B1", "B2", "E2", "B4", "##", "##",,,,]; . . By this, the source ranges are automatically created by var srcRanges = [...Array(dstRnages.length)].map((_, i) => ( 'Data'!${columnIndexToLetter_(i)}${row} ));这样,源范围由var srcRanges = [...Array(dstRnages.length)].map((_, i) => ( 'Data'!${columnIndexToLetter_(i)}${row} 自动创建)); . . If you want to change this, please modify this.如果你想改变这个,请修改这个。

function searchRecord() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var templateS = ss.getSheetByName("Template");
  var dataS = ss.getSheetByName("Data");
  var searchCell = templateS.getRange("B6");
  var searchValue = searchCell.getValue();
  var recordFound = dataS.getRange("A:A")
    .createTextFinder(searchValue)
    .matchCase(true)
    .matchEntireCell(true)
    .findNext();

  // I modified the below script.
  if (!recordFound) return;
  var dstRnages = ["D3", "B1", "B2", "E2", "B4"]; // This is from your script.

  // Ref: https://stackoverflow.com/a/53678158
  const columnIndexToLetter_ = index => (a = Math.floor(index / 26)) >= 0 ? columnIndexToLetter_(a - 1) + String.fromCharCode(65 + (index % 26)) : "";

  var row = recordFound.getRow();
  var ssId = ss.getId();
  dstRanges = dstRnages.map(e => `'Template'!${e}`);
  var srcRanges = [...Array(dstRnages.length)].map((_, i) => (`'Data'!${columnIndexToLetter_(i)}${row}`));
  var data = Sheets.Spreadsheets.Values.batchGet(ssId, { ranges: srcRanges }).valueRanges.map(({ values }, i) => ({ values, range: dstRanges[i] }));
  Sheets.Spreadsheets.Values.batchUpdate({ data, valueInputOption: "USER_ENTERED" }, ssId);
}
  • When this script is run, the values are retrieved from srcRanges , and the retrieved values are put to dstRanges .运行此脚本时,将从srcRanges中检索值,并将检索到的值放入dstRanges中。

Note:笔记:

  • In this modification, it supposes that your showing script works fine.在此修改中,它假设您的显示脚本工作正常。 Please be careful about this.请注意这一点。

References:参考:

暂无
暂无

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

相关问题 Google Apps 脚本 - 尝试有效地将不连续的单元格从一张纸复制到另一张纸的第一个空行 - Google Apps Script- Trying to Efficiently Copy Noncontiguous Cells From One Sheet to a First Empty Row of Another Google Spreadsheet-如何将单元格数据从一张工作表复制到另一张工作表中的单元格? - Google Spreadsheet - How to copy cell data from one sheet to a cell in another sheet? 用于将数据从一张纸传输到另一张纸的 Google 脚本 - Google Script to transfer Data from one sheet to another 将单元格数据从一张Google表格发送到另一张表格,然后覆盖数据 - Send cell data from one Google sheet to another and then overwrite data 将谷歌工作表中的数据复制到另一个谷歌工作表的最后一行 - Copy data from a google sheet to the last row in another google sheet 如何将一个Google工作表复制到另一个Google工作表 - How to copy one google sheet to different another google sheet 从母版工作表的最后一行复制数据,并将其粘贴到Google工作表中另一工作表的特定单元格 - Copy data from last row of master sheet and paste it in specific cells in another sheet in google sheets 谷歌表根据2个不同单元格中的2个值将单元格复制到另一个选项卡 - google sheet copy cells to another tab according to 2 values in 2 different cell Google文件-指令码,将储存格从一张工作表复制到另一张工作表 - Google Docs - Scripts, copy cell from one sheet to another sheet Google Sheet Email 脚本到 email 在不同单元格中编辑的行的多个单元格 - Google Sheet Email script to email multiple cells of row that was edited in a different cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM