如何使用Google脚本将电子表格复制到GDrive上的特定文件夹中,而无需复制相关的表单和脚本

时间:2014-11-06 10:02:18

标签: excel forms google-apps-script

我创建了一个将条目记录到Google表格中的表单。每天一次,我希望将电子表格作为附件通过电子邮件发送给我们的技术人员。同时,我想在特定文件夹中制作电子表格的副本,作为"记录"。我使用以下代码

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());  
}

我遇到的问题是当" old"将电子表格复制到新文件夹中,显然相关脚本也会复制并链接到电子表格的表单副本。有没有办法只复制电子表格(即单元格中的数据),没有所有附加的" frills"。

干杯, 尼古拉

1 个答案:

答案 0 :(得分:1)

我从没想过会发生这个问题(复制相关的表格,呃!),我还没有尝试过确认。但由于您只想要这些值,我认为将电子表格内容复制到新的电子表格可能更容易。

//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