[英]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.我相信你的目标如下。
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 用于您的脚本时,如何进行以下修改?
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);
}
srcRanges
, and the retrieved values are put to dstRanges
.运行此脚本时,将从srcRanges
中检索值,并将检索到的值放入dstRanges
中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.