JimmyS
JimmyS

Reputation: 17

How do I clear a specific or range of cells on a different google sheets workbook?

I have two workbooks. Workbook A and Workbook B.

I want to have a menu item (or button) on Workbook A, that clears the contents of a specific cell or range of cells on Workbook B.

Thus far, on Workbook A, I have a good script that will clear only a cell on Workbook A itself.

  function onOpen() {
    SpreadsheetApp.getUi()
   .createMenu('Reset')
   .addItem('Based on a permanent ranges address list', 'userActionResetByRangesAddresses')
   .addToUi();
   }
  function clearRange1() { 
    var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
    sheet.getRange('D2').clearContent();
   }

So this script adds a menu item on Workbook A, and clears the content of cell D2 on Workbook A.

I'd like the menu item to be on Workbook A, but clear the cell on Workbook B. So basically I use a menu item on Workbook A, it pulls Sheet1 on Workbook B and clears the cell.

I've been hacking at this for a while and am stumped. :)

Thanks!

Upvotes: 0

Views: 78

Answers (1)

ziganotschka
ziganotschka

Reputation: 26806

You need to modify

var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');

to

var sheet = SpreadsheetApp.openById("ID OF WORKBOOK B").getSheetByName('Sheet1');,

see documentation.

Now you will be able to apply your script to any workbook - just paste it's ID.

Upvotes: 1

Related Questions