[英]Google Apps Script Copying a column in one sheet, transposing it, then pasting to another
I have a sheet that is set up in the template of a form, with data prompts like Name, ID#, etc. in column A and the actual data that is inputted in column B. I have created a button labeled 'Submit Form' that is linked to the script. 我有一个在表单模板中设置的工作表,在A列中有数据提示,例如Name,ID#等,在B列中输入了实际数据。我创建了一个标有“提交表单”的按钮链接到脚本。 What I want this script to achieve is to copy only the data from a specific range in column B, then paste that data into the next empty row in a new sheet to create a sort of database of the form responses.
我希望此脚本实现的是仅复制B列中特定范围内的数据,然后将这些数据粘贴到新工作表的下一个空行中,以创建一种表单响应的数据库。 It will also clear the data from the range in column B on the original sheet.
还将清除原始工作表B列中的数据。
I already have a way to clear the selected range on the original sheet, as well as a way to copy the selected range to the new sheet while automatically starting from the first empty row. 我已经有一种方法可以清除原始工作表中的选定范围,也可以将选定范围复制到新工作表中,同时自动从第一行开始。 I am having trouble transposing the data, however, since it pastes into a column like the original data, as opposed to pasting into a row.
但是,我在转换数据时遇到了麻烦,因为它像原始数据一样粘贴到列中,而不是粘贴到行中。
function submitForm() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("RMA");
var sheet2 = ss.getSheetByName('RMA Database');
var values = sheet2.getRange("A:A").getValues();
var maxIndex = values.reduce(function(maxIndex, row, index) {
return row[0] === "" ? maxIndex : index;
}, 0);
sheet2.setActiveRange(sheet2.getRange(maxIndex + 2, 1))
sheet1.getRange("B5:B25").copyTo(sheet2.getRange(sheet2.getLastRow()+1,1),
{contentsOnly:true});
sheet1.getRange('B5:B25').clearContent();
}
Try this: 尝试这个:
I don't see the point of this line var values = sheet2.getRange("A:A").getValues();
我看不到这条线的意义
var values = sheet2.getRange("A:A").getValues();
so I just omitted it 所以我省略了
function runOne() {
var ss = SpreadsheetApp.getActive();
var sheet1 = ss.getSheetByName("RMA");
var sheet2 = ss.getSheetByName('RMA Database');
var vA=sheet1.getRange("B5:B25").getValues().map(function(r){return r[0]});
sheet2.appendRow(vA);
sheet1.getRange('B5:B25').clearContent();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.