简体   繁体   中英

How to trigger Google Apps script when user makes a copy

I want to clear some range content from a sheet when someone creates a copy of that g-sheet.

How can I make the "create a copy" menu item, trigger a script?

在此处输入图像描述

I disabled the "make a copy" menu item and allow it only by a script.

function makeACopy() {
  clearRange("A1:A100");
  var documentId = DriveApp.getFileById('FileId').makeCopy().getId();
  DriveApp.getFileById(documentId).setName('PT generator' + new Date() );
};

function clearRange(rangeToClear) {
    const sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
    const formulas = sheet.getRange(rangeToClear).getFormulas();

    sheet.getRange(rangeToClear).clear({
        contentsOnly: true,
        skipFilteredRows: false
    });
    sheet.getRange(rangeToClear).setFormulas(formulas)
};

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