简体   繁体   中英

Delete Cells in sheets via script that is triggered from another sheet. [google sheet script]

For a spreadsheet that contains several sheets, I have a script with which I can delete data in all sheets from certain ranges at the push of a button.

Now I have a main spreadsheet that is linked to several other spreadsheets (and the sheets they contain) (import range, queries, etc.).

Is it possible to delete data in certain ranges by script at the push of a button in the connected spreadsheets, whereby I would like to trigger this from the main spreadsheet?!

The data that is to be deleted from the sheets using the script is manual user input and therefore does not come from the main spreadsheet. The Ranges/Cells that should be deleted are different between Sheet 1.A and 1.B

Attached a graph for better understanding:

图形

My script so far is:

    function onOpen() {

  SpreadsheetApp.getUi()

    .createMenu('Delete User Input Data')

    .addItem(

      'Delete Data now',

      'userActionResetMultipleSheetsByRangesAddresses'

    )

      .addToUi();

}

function userActionResetMultipleSheetsByRangesAddresses() {

  var sheetNames = [

    { name: 'Sheet1.A', rangesAddressesList: ['Y3:AG99'] },

    { name: 'Sheet1.B', rangesAddressesList: ['AI3:AX99'] },

    ];

  sheetNames.forEach(function(sn) {

    var sheet = SpreadsheetApp.getActive().getSheetByName(sn.name);

    if (sheet) {

      resetByRangesList_(sheet, sn.rangesAddressesList);

    }

  });

}

function resetByRangesList_(sheet, rangesAddressesList) {

  sheet.getRangeList(rangesAddressesList).clearContent();

}

I think you could use something like this:

function userActionResetMultipleSheetsByRangesAddresses() {
  var ss=[{id:'',name:'Sheet1.A', rangesAddressesList: ['Y3:AG99'] },{id:'',name:'Sheet1.B',rangesAddressesList: ['AI3:AX99'] },
          {id:'',name:'Sheet2.A', rangesAddressesList: ['Y3:AG99'] },{id:'',name:'Sheet2.B',rangesAddressesList: ['AI3:AX99'] },
          {id:'',name:'Sheet3.A', rangesAddressesList: ['Y3:AG99'] },{id:'',name:'Sheet3.B',rangesAddressesList: ['AI3:AX99'] }
         ];
  ss.forEach(function(e) {
    var sheet = SpreadsheetApp.openById(e.id).getSheetByName(e.name);
    if(sheet){
      sheet.getRangeList(e.rangesAddressesList).clearContent();
    }
  });
}

You will just have to add the spreadsheet ids and correct the rest of the sheet names and ranges.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM