将模板电子表格复制到文件夹,然后写入新副本(谷歌脚本)

时间:2018-01-12 09:12:00

标签: javascript google-apps-script copy

我需要将模板电子表格复制到文件夹,然后在新副本中写入C3。

//get template 
var SStemplate = "URL here";
var SStocopy = SpreadsheetApp.openByUrl(SStemplate); 

//set destination folder
var SSdestFolder = DriveApp.getFolderById("0B-hjh1DycmoweEl5ZmgxdFBRYzg");

//copy template to destination 
DriveApp.getFileById(SStocopy.getId()).makeCopy(name, SSdestFolder);

创建副本后,如何激活它以设置单元格C3的值?

这是最终的工作版本:

function newprojectdocuments(){     
//activate current sheet to get new project ID
var trackerss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = trackerss.getSheetByName('AutoTracker');
var range = sheet.getRange('F1:F1');
var foldername = range.getValues();

if( foldername.toString().length === 7 ){       
//get template spreadsheet to make copy
var SStemplate = "url";
var SStocopy = SpreadsheetApp.openByUrl(SStemplate);  
//set destination folder for new project spreadsheet
var SSdestFolder = DriveApp.getFolderById("ID");    
//copy template to the spreadsheet folder
var newFile = DriveApp.getFileById(SStocopy.getId()).makeCopy(foldername, 
SSdestFolder);  
//open the new file
var ss = SpreadsheetApp.openById(newFile.getId());
var sheet = ss.getSheetByName('Overview');
//Write the value you want in C3 cell
sheet.getRange("C3").setValue(foldername);
//create new folder in deal documents folder
var dealfolder=DriveApp.getFolderById("ID");
var newfolder=dealfolder.createFolder(foldername)  
if( foldername.toString().length === 7 ){
Browser.msgBox( "Project " + foldername + " folder & spreadsheet created! 
Refresh the tracker to display."  )
 }
 } else {
Browser.msgBox('Project ID must be 7 digits: YYMMDDN format') 
} 
}

1 个答案:

答案 0 :(得分:0)

makeCopy() method返回新文件。用它来进去就像:

block content