复制行并删除

时间:2018-07-17 06:49:31

标签: google-apps-script google-sheets

我有2张纸-“表格回复1”和“学生”。

当我运行下面的函数时,它将所有数据从“表单响应1”复制到“学生”,在第二张工作表中,我将需要对数据进行进一步处理。复制后,我希望从“表单响应1”中删除该特定行。在下一轮我再次运行该函数时(“表单响应1”中有新数据时),它将相应地追加到“学生”中。

非常感谢您能在下面建议我如何修改我的代码,谢谢!

function Copy() 
{
    var sss = SpreadsheetApp.openById('sheet ID'); //replace with source ID
    var ss = sss.getSheetByName('Form Responses 1'); //replace with source Sheet tab name
    var range = ss.getRange('A:V'); //assign the range you want to copy
    var data = range.getValues();
    var numRows = range.getNumRows();
    var getRow = range.getRow();

    var tss = SpreadsheetApp.openById('sheet ID'); //replace with destination ID
    var ts = tss.getSheetByName('Students');  

    ts.getRange(1, 1, data.length, data[0].length).setValues(data);

    /*delete function is missing*/
} 

1 个答案:

答案 0 :(得分:0)

下面的代码应该可以达到期望的结果,否则这是一个不错的起点。该代码非常简单,因此我将所有解释都放在了代码的注释中。

要使用此代码,您可以创建一个独立脚本。 (不需要绑定到电子表格。)将主要和辅助代码复制到两个单独的标签中。 (请参见所附图片。)在两个脚本中更改了ID后,请运行主脚本,您将看到所需的结果。 (请注意,表单响应中的所有数据都将被清除。)

希望这会有所帮助,并随时询问您是否不确定。请注意,此时我将指导您使用文档,而不是试图解释某些方法/调用的工作方式:

主要代码

function copy_Delete(){

var ss, sheet;
var lastRow, lastColumn;
var responseData;

//Get Spreadsheet and Sheet name for Form Responses Sheet  
ss = SpreadsheetApp.openById("YOUR_ID_HERE");
sheet = ss.getSheetByName("Form Responses 1");

//Gets a numerical value representing the last row and last column where data is entered.  
lastRow = sheet.getLastRow();
lastColumn = sheet.getLastColumn();

//Get all the data in the From Responses Sheet and put it all in an array.
responseData = sheet.getRange(2, 1, lastRow -1, lastColumn).getValues();

//Call the destination function and pass the array to it. Then clear form responses of all data.
destination(responseData);
sheet.clear();
}

二级代码

    function destination(responseData){

    var ss, sheet, form;
    var numRows, numColumns, lastRow;

    //Get Spreadsheet and Sheet name for your destination sheet 
    ss = SpreadsheetApp.openById("YOUR ID HERE");
    sheet = ss.getSheetByName("Destination Sheet");

    //Get the dimensions of the array and the last row of the spreadsheet.
    numRows = responseData.length;
    numColumns = responseData[0].length;
    lastRow = sheet.getLastRow();

    //Using the dimensions above,we push our array from the previous function into our new sheet.
    sheet.getRange(lastRow +1, 1, numRows, numColumns).setValues(responseData);

    //Get form and delete all responses.
    form = FormApp.openById('YOUR_FORM_ID_HERE');
    form.deleteAllResponses();
    }

附加示例

enter image description here