简体   繁体   中英

How can I copy a spreadsheet into specifc folder on GDrive using Google Script WITHOUT copying associated Form and Script

I created a form that logs entries into a Google Sheet. Once a day, I want the spreadsheet to be emailed, as an attachment, to our technician. At the same time I want to make a copy of the spreadsheet, as a "record", in a specific folder. I use the following code

function sendEmail() {

var oauthConfig = UrlFetchApp.addOAuthService("google");
oauthConfig.setAccessTokenUrl("url");
oauthConfig.setRequestTokenUrl("url");
oauthConfig.setAuthorizationUrl("url");
oauthConfig.setConsumerKey("anonymous");
oauthConfig.setConsumerSecret("anonymous");

var requestData = {"method": "GET", "oAuthServiceName": "google", "oAuthUseToken": "always"};

var url = "url";

var result = UrlFetchApp.fetch(url , requestData);  
var contents = result.getContent();

var cur_date = Utilities.formatDate(new Date(), "GMT+1", "yyyy/MM/dd")

MailApp.sendEmail("bubu@hotmail.com","test" ,"test", {attachments:[{fileName:cur_date + "_Orders.html", content:contents, mimeType:"application/html"}]});
var spreadsheet = DriveApp.getFileById("ID")
spreadsheet.makeCopy(cur_date + "_Orders", DriveApp.getFoldersByName("Orders").next());  
}

The problem I have is that when the "old" spreadsheet is copied, into the new folder, obviously the associated script is also copied alongside and a copy of the form linked to the spreadsheet. Is there a way to copy the spreadsheet only (that is, the data in the cells), without all the attached "frills".

Cheers, Nicola

I never imagined this problem would happen (copying the associated form, ugh!), and I haven't tried it to confirm. But since you want only the values, I think it's probably easier to copy just the spreadsheet contents to a new spreadsheet.

//replacing your last two lines of code
var spreadsheet = SpreadsheetApp.openById("ID");
//this assumes you want the values only of the first sheet
//and that you don't care about formatting or sheets names, etc
//but you have more options here if this is not desired
var values = spreadsheet.getSheets()[0].getDataRange().getValues();
var copy = SpreadsheetApp.create(cur_date + "_Orders");
copy.getSheets()[0].getRange(1,1,values.length,values[0].length).setValues(values);

var copyAsFile = DriveApp.getFileById(copy.getId());
//I'd recommend using the folder ID instead
DriveApp.getFoldersByName("Orders").next().addFile(copyAsFile);
DriveApp.removeFile(copyAsFile); //remove from your Drive root folder

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