Modify Code - Copy Range to another Spreadsheet Google Sheets

后端 未结 1 1537
后悔当初
后悔当初 2020-12-21 18:24

The code below copies range and moves to next Spreadsheet in same workbook called \"Topup Required\".

I would like it to send this data to another spreadsheet inside

相关标签:
1条回答
  • 2020-12-21 19:02
    • You want to copy the values and format from s.getRange(row, 1, 1, numColumns) to the last row on the sheet of TOP UP NEEDED in the Spreadsheet of sheet ID, when the cell is edited.

    If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

    Modification point:

    Unfortunately, in the current stage, copyTo() of Class range cannot be used for other Spreadsheet.

    Pattern 1:

    In this pattern, copyTo() is used. So both the values and the cell format are copied.

    Modified script:

    When your script is modified, please modify as follows.

    From:
    var targetSheet = ss.getSheetByName("TOP UP NEEDED");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).copyTo(target);
    
    To:
    var targetSS = SpreadsheetApp.openById("sheet ID");
    s = SpreadsheetApp.getActiveSheet();
    var tempSheet = s.copyTo(targetSS);
    var targetSheet = targetSS.getSheetByName("TOP UP NEEDED");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    tempSheet.getRange(row, 1, 1, numColumns).copyTo(target);
    targetSS.deleteSheet(tempSheet);
    

    Pattern 2:

    In this pattern, getValues() and appendRow() are used. So only the values are copied.

    Modified script:

    When your script is modified, please modify as follows.

    From:
    var targetSheet = ss.getSheetByName("TOP UP NEEDED");
    var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
    s.getRange(row, 1, 1, numColumns).copyTo(target);
    
    To:
    var targetSheet = SpreadsheetApp.openById("sheet ID").getSheetByName("TOP UP NEEDED");
    var sourceValues = s.getRange(row, 1, 1, numColumns).getValues()[0];
    targetSheet.appendRow(sourceValues);
    

    Note: IMPORTANT

    • In this modification, the simple trigger of onEdit() cannot be used. An error occurs at openById(). So please use the installable trigger of OnEdit event trigger. In this case, please modify the function name as follows.

      • From

        function onEdit(event) {
        
      • To

        function installedOnEdit(event) {
        
      • After this modification, please install the OnEdit event trigger to installedOnEdit as the installable trigger. By this, when the cell is edited, installedOnEdit is run without the double executions. Please be careful this.

    References:

    • copyTo(destination) of Class Range
    • copyTo(spreadsheet) of Class Sheet
    • getValues()
    • appendRow(rowContents)
    • Simple Triggers
    • Installable Triggers

    If I misunderstood your question and this was not the direction you want, I apologize.

    0 讨论(0)
提交回复
热议问题