0

I am using google Sheets

I want to do the following but I cant figure out how:

We do have like a daily Report sheet. We need it for every day, so I create 31 new Sheettabs 1 for each day and give the whole Spreadsheet the name of the Month.

I want to automatize it because its way too much work to copy like every day for every month over and over. Is there a script to do so? Its enough for me that I have like 1 Button to start the Script and its copying the first day as a template and paste it 30 times, so it would be easy to do some changes aswell.

thanks!

3
  • 2
    Welcome! Sorry, but you have chosen the wrong approach. And it's very good that you asked the question before you had time to develop this wrong approach. The fact is that your next problem will be to collect data from all this sheets into one table to be able to analyze the accumulated data for certain periods - a month, a quarter, and so on. The correct way is to immediately accumulate data in one sheet, a long table that will contain the date and report data. So that the user does not get confused in this long table, you will give him the opportunity to enter data using Google forms.
    – JohnSUN
    Commented Dec 15, 2022 at 10:21
  • @JohnSUN With respect, your advice is wrong. The OP hasn't described the data recorded or the layout. Yet you advise against the OP's scenario and say that the "correct way" is to accumulate data in a single sheet using Google Forms. The use of "correct" is inappropriate - implying "best practice" rather than your personal opinion. Also building dynamic links between a summary sheet and data sheets is not a technical problem. In short, the basis for your advice has no foundation.
    – Tedinoz
    Commented Dec 23, 2022 at 5:38
  • @Tedinoz With respect, precisely because this is just my humble opinion and not a directive, I didn't propose this as an answer, but posted it as a comment, as a warning of possible problems in the future. To be clear, I didn't say that Forms are the only and mandatory way to enter information. My remark concerned exclusively the recommended way of organizing information - one single table on one sheet for the accumulation and storage of data.
    – JohnSUN
    Commented Dec 23, 2022 at 5:57

1 Answer 1

1

Use Sheet.copyTo(), like this:

function run() {
  manageSheets_({
    action: 'insert',
    monthName: 'December',
    numDays: 31,
  });
}

function manageSheets_({ action = 'insert', monthName, numDays = 31, masterSheet, ss }) {
  ss = ss || SpreadsheetApp.getActive();
  monthName = monthName || ss.getName();
  masterSheet = masterSheet || ss.getSheets()[0];
  for (let day = 2; day <= numDays; day++) {
    const sheetName = `${day} ${monthName}`;
    if (action === 'delete') {
      const sheet = ss.getSheetByName(sheetName);
      ss.setActiveSheet(sheet);
      ss.deleteActiveSheet();
    } else {
      masterSheet
        .copyTo(ss)
        .setName(sheetName);
    }
  }
}

If you need to delete the newly created sheets for some reason, replace action: 'insert' with action: 'delete' in the run() function.

You can assign the run() function to a button.

1
  • I was going to suggest a custom menu, but this would be just as risky as a button if other users (consciously or not) ran the function. Since the admin has to edit "monthName" and "numDays" in the Project Editor (and, of course, these values could be read from the spreadsheet itself), maybe there is 'simple' edit to manageSheet_() to avoid running the function unintentionally. ss = ss || SpreadsheetApp.getActive(), if (ss.getSheets().length >= numDays && action === 'insert') { return }, if (ss.getSheets().length <= numDays && action === 'delete') { return }.
    – Tedinoz
    Commented Dec 28, 2022 at 2:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.