简体   繁体   English

Google电子表格将值从一行复制到另一张工作表

[英]Google spreadsheet copy values from one row to another sheet

I have a Google Spreadsheet with 3 different sheets that you can see here . 我有一个Google电子表格,其中包含3种不同的表格,您可以在此处看到。

What I would like to do is per each person in the 'person' sheet, create a row of the person in the 'stacs' sheets that will be repeated as many times based of the number of 'meals' that we have in the 'meals' sheet. 我想做的是在“人员”表中的每个人,在“ stacs”表中创建一行人,根据我们在“人员”表中拥有的“餐”的数量将其重复多次。餐单。 So, per each person, I want to create personA + meal1 in row #1, personA + meal2 in row #2, personA + meal3 in row #3, etc. 因此,对于每个人,我想在第1行中创建personA +餐1,在第2行中创建personA +餐2,在第3行中创建personA +餐3,等等。

I figured it out how to do this repeating the same code over and over again. 我想出了如何重复执行相同的代码。 The code works but it's very inefficient. 该代码可以工作,但是效率很低。 I guess that there has to be a way of doing the same repetition that I'm doing in a much cleaner and simpler way. 我猜必须要有一种方法,可以以更简洁,更简单的方式进行与我相同的重复。 I'm just learning how to you use some scripting to improve my spreadsheet so any help with this will be very welcome! 我只是在学习如何使用一些脚本来改善我的电子表格,因此非常欢迎您提供任何帮助!

Some Caveats: 一些警告:

  • If you take a look at the spreadsheet, you will see that I'm using formulas in some cell to calculate the last row with value or the sizes of the ranges. 如果您看一下电子表格,您会发现我在某些单元格中使用公式来计算具有值或范围大小的最后一行。
  • In the 'meals' tab I have defined a name range with the meals. 在“餐食”标签中,我定义了餐食的名称范围。 I'm not very proud of this, because the list of the meal will grow so I wonder if there is a more dynamic way of dealing with this instead of adjusting the range every time I add a new meal. 我对此并不感到很自豪,因为进餐的数量会增加,所以我想知道是否有一种更动态的方式来处理此问题,而不是每次添加新餐时都调整范围。

Here is the code that you also find in the spreadsheet: 您也可以在电子表格中找到以下代码:

 function copyValuesA() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();  
 var sheet = ss.getSheetByName("names");
 var name = sheet.getRange("A2");
 var sheetMeals = ss.getSheetByName("meals");
 var rangeMeals = sheet.getRange("mealsNames");
 var sheetstacs = ss.getSheetByName("stacs");
 var lastRow = sheetstacs.getRange("B1").getValue();
 var mealCount = sheetstacs.getRange("D1").getValue();
 var lastRowMeals = sheetstacs.getRange("C1").getValue();
 var rangeOrigname01 = sheetstacs.getRange("A" + lastRow + ":A" + mealCount);
 var rangeOrigname02 = sheetstacs.getRange("A1");

 name.copyTo(rangeOrigname01);

 var rangeDestMeals01 = sheetstacs.getRange("B" + lastRowMeals);

 rangeMeals.copyTo(rangeDestMeals01);

 }


function copyValuesB() {
var ss = SpreadsheetApp.getActiveSpreadsheet();  
var sheet = ss.getSheetByName("names");
var name = sheet.getRange("A3");
var sheetMeals = ss.getSheetByName("meals");
var rangeMeals = sheet.getRange("mealsNames");
var sheetstacs = ss.getSheetByName("stacs");
var lastRow = sheetstacs.getRange("B1").getValue();
var mealCount = sheetstacs.getRange("D1").getValue();
var lastRowMeals = sheetstacs.getRange("C1").getValue();
var rangeOrigname01 = sheetstacs.getRange("A" + lastRow + ":A" + mealCount);
var rangeOrigname02 = sheetstacs.getRange("A1");

name.copyTo(rangeOrigname01);

var rangeDestMeals01 = sheetstacs.getRange("B" + lastRowMeals);

rangeMeals.copyTo(rangeDestMeals01);   
}


function copyValuesC() {

var ss = SpreadsheetApp.getActiveSpreadsheet();  
var sheet = ss.getSheetByName("names");
var name = sheet.getRange("A4");
var sheetMeals = ss.getSheetByName("meals");
var rangeMeals = sheet.getRange("mealsNames");
var sheetstacs = ss.getSheetByName("stacs");
var lastRow = sheetstacs.getRange("B1").getValue();
var mealCount = sheetstacs.getRange("D1").getValue();
var lastRowMeals = sheetstacs.getRange("C1").getValue();
var rangeOrigname01 = sheetstacs.getRange("A" + lastRow + ":A" + mealCount);
var rangeOrigname02 = sheetstacs.getRange("A1");

name.copyTo(rangeOrigname01);

var rangeDestMeals01 = sheetstacs.getRange("B" + lastRowMeals);

rangeMeals.copyTo(rangeDestMeals01);   
 }


function copyValuesAll() {

copyValuesA();
copyValuesB();
copyValuesC();
 }

If your interested in a dynamic formula to accomplish this: 如果您对动态公式感兴趣,请执行以下操作:

in your stacs sheet in column A cell 2 put: 在第2单元格的stacs表中:

=TRANSPOSE(SPLIT(JOIN(";",ARRAYFORMULA(REPT(A2:A&";",COUNTA(meals!$A$2:$A)))),";"))

and in cell B2: 并在单元格B2中:

=TRANSPOSE(SPLIT(ARRAYFORMULA(REPT(JOIN(";",meals!$A$2:$A$8),COUNTA(names!A2:A))),";"))

doing this will automatically account for new values 这样做将自动考虑新值

在此处输入图片说明

暂无
暂无

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

相关问题 将一行从一个Google表格电子表格复制到表单提交中的另一表格 - Copy a row from one Google Sheet Spreadsheet to Another on Form Submit 将原始数据从工作表“源”复制到另一个工作表“目的地”,并在Google电子表格行的第一列中添加日期 - Copy a raw from sheet 'source' to another sheet 'destination' adding the date in the first column of the row Google spreadsheet 如何使用谷歌应用脚本将一行从一个谷歌电子表格复制到另一个谷歌电子表格? - How to copy a row from one google spreadsheet to another google spreadsheet using google apps script? Google Script:根据值将行从一张纸复制到另一张纸 - Google Script: Copy row from one sheet to another depending on value 根据条件将单元格值从一个Google工作表复制到另一个 - Copy cell values from one google sheet to another based on condition 将工作表复制到另一个电子表格[Google Apps脚本] - Copy sheet to another spreadsheet [Google Apps Script] Google 工作表脚本将数据集从一张工作表复制到另一张工作表的顶部(仅限值) - Google sheets script copy data sets from one sheet to another (values only) at the top of the sheet 将范围从一个工作表复制到Google工作表中的另一个工作表 - Copy a range from one sheet to another in google sheet Google脚本会根据日期将范围从一个电子表格复制到另一个电子表格 - Google scripts copy range from one spreadsheet to another based on date 将过滤后的范围从一个电子表格复制到另一个 - Google 应用脚本 - Copy filtered range from one spreadsheet to another - Google app script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM