简体   繁体   中英

Having trouble save my output file as name based on cell from spreadsheet in google apps scripts

Here's my code which was done mostly by a much talented coder then me:

function SaveSpreadsheetAs(){
  
var Sheet=SpreadsheetApp.getActiveSpreadsheet();
var Peter=Sheet.getSheetByName("Peter");        
var Filename=Peter.getRange(2,2,1,1).getValue();
  //Folder to save pay periods on google drive
 var destFolder = DriveApp.getFolderById("es3424kbjb4j32wbryujb3r"); 
              
 DriveApp.getFileById("093hnweuibf2873bfuiww").makeCopy(Filename,destFolder); 

 }

My question is why is (var Filename)'s output not the value of the B2 cell from the sheet name "Peter"? I know I am making a very basic level mistake as I am very new to this but what am I missing. The code works if you change the (var Filename) with a text entry. Thank you in advanced.

I ran it this way. It runs fine.

function SaveSpreadsheetAs() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sh = ss.getSheetByName(gobj.globals.targetsh);
  const Filename = Utilities.formatDate(new Date(sh.getRange(2, 2).getValue()),ss.getSpreadsheetTimeZone(),"MMM-dd-yyyy HH:mm:ss");
  const destFolder = DriveApp.getFolderById(gobj.globals.testfolderid);
  const file=DriveApp.getFileById(gobj.globals.testsourceid).makeCopy(Filename, destFolder);
  Logger.log(file.getName());
}

Execution log
5:40:56 PM  Notice  Execution started
5:41:01 PM  Info    Apr-25-2021 17:40:41
5:41:01 PM  Notice  Execution completed

gobj is a global object I use so that I don't have to remove my ids

Data Sheet:

4/25/2021 17:40:41

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