Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-macros

Clear a cell using a macro in a separate sheet


I would like to create a macro in sheets that would open a different spreadsheet and clear data from certain cells. I can do it in the same spreadsheet, but haven't been able to figure out how to do it when I have spreadsheet 'A' open and want to delete information on spreadsheet 'B'


Solution

  • You can use either SpreadsheetApp.openById('{your_sheet_id}') (see documentation) or SpreadsheetApp.openByUrl('{your_sheet_url}') (see documentation).

    Then you can chain with the usual getSheet(), getRange() and clear() or clearContent() methods, e.g.:

    SpreadsheetApp.openById('xxxxxxxxx').getSheetByName('Sheet1').getRange('A4').clearContent();