简体   繁体   English

Google Apps脚本在一个工作表中复制一列,将其转置,然后粘贴到另一个

[英]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.

相关问题 Google表格根据单元格值从一张表格复制和粘贴到另一张表格 - Google Sheets Copying and Pasting from one sheet to another based on cell value Google脚本将单元格的一部分复制到另一个工作表 - Google Script Copying part of a cell to another sheet 将工作表复制到另一个电子表格[Google Apps脚本] - Copy sheet to another spreadsheet [Google Apps Script] 使用 Google App 脚本将数据从一张工作表复制到另一张工作表时执行缓慢 - Slow Execution for Copying Data from One Sheet to Another Using Google App Script 什么是正确的Google脚本,用于动态复制更新范围并粘贴到存档表中? - What would be a proper Google Script for copying dynamically updating range and pasting in Archive sheet? Google Apps脚本-从另一个工作表导入工作表值 - Google apps script - Importing sheet values from another sheet Google Apps Script - onEdit 使用数组将数据从一张纸复制到另一张纸 - Google Apps Script - onEdit using an Array to Copy data one sheet to another 使用Google Apps脚本复制数据 - Data copying with Google Apps Script Google Apps 脚本 - 想要截断表单中特定列中的文本 - Google Apps Script - Want to truncate a text in a particular column in a form sheet 按 Apps 脚本中的列名读取 Google Sheet Exported JSON - Read Google Sheet Exported JSON by column name in Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM