0

The following code creates "new spreadsheet" and set "test" to cell A1 and copies the spreadsheet to "copied spreadsheet".

But "test" of cell A1 is set only to "new spreadsheet" but "copied spreadsheet". Is there some save() method to save a spreadsheet modified by gas?

  // create spreadsheet named "new spreadsheet"
  const spreadsheet = SpreadsheetApp.create('new spreadsheet');

  const sheet = spreadsheet.getActiveSheet();

  // set "test" to cell A1
  sheet.getRange(1, 1).setValue('test');
  
  // copy "new spreadsheet" to "copied spreadsheet"
  // *but cell A1 of "copied spreadsheet" is unexpectedly blank.
  DriveApp.getFileById(spreadsheet.getId()).makeCopy('copied spreadsheet');

What I really want to do is to modify spreadsheet cells and send it as an attachment file of a mail. But the same issue I wrote above happens. The modifications to cells are not applied to the attached spreadsheet.

  GmailApp.sendEmail(
    to,
    subject,
    body,
    {
      // *modifications to the spreadsheet by gas are ignored
      attachments: spreadsheet,
    }
  );

user_
  • 852
  • 7
  • 23
  • 1
    Please put `SpreadsheetApp.flush()` before the line of `DriveApp.getFileById(spreadsheet.getId()).makeCopy('copied spreadsheet')` and test it again. – Tanaike Feb 11 '22 at 07:34
  • Thank you! SpreadsheetApp.flush() fixed the issue. – user_ Feb 11 '22 at 07:48
  • 1
    Thank you for replying. I'm glad your issue was resolved. There are several duplicated questions of this situation. So I flagged this as a duplicated question. – Tanaike Feb 11 '22 at 07:48
  • Thank you for flagging. The already answered question was difficult to find for me. The already answered question is exactly very similar to my situation. – user_ Feb 11 '22 at 07:53

0 Answers0