Search code examples
google-apps-scriptgoogle-sheets

Is it possible to copy an image from merged cells into one cell using Google Apps Script?


I want to copy/move an image from a merged cells to another sheet in one cell using Google Apps Script.

So far i've tried: getValue() and setValue() - nothing happens copyTo() - the image retains its original size moveTo() - same result as copyTo()

My current code for this:

```
var sourceSheet = SpreadsheetApp.getActive().getSheetByName('Source');
var destinationSheet = SpreadsheetApp.getActive().getSheetByName('Destination');

function myFunction() {
  sourceSheet.getRange('B6:B16').copyTo(destinationSheet.getRange(1, 2));
}
```

An example spreadsheet: https://docs.google.com/spreadsheets/d/1Bqf08o_DtUw1lMpjA4xAeHJQWlPNVadj9E8bRs11SfU/edit

My goal is that the image in the destinationSheet is only in one cell and not multiple cells. I don't know what else I can try and I hope someone can help me.


Solution

  • Although I'm not sure whether I could correctly understand your expected result, how about the following modification?

    From:

    function myFunction() {
      sourceSheet.getRange('B6:B16').copyTo(destinationSheet.getRange(1, 2));
    }
    

    To:

    function myFunction() {
      sourceSheet.getRange('B6').copyTo(destinationSheet.getRange(1, 2));
    }
    

    or

    function myFunction() {
      var dstRange = destinationSheet.getRange(1, 2);
      sourceSheet.getRange('B6:B16').copyTo(dstRange);
      dstRange.getMergedRanges()[0].breakApart();
    }
    
    • By this, an image of the merged cells of "B6:B16" is put into cell "B1" of destinationSheet.