使用谷歌脚本将创建的文件从模板移动到文件夹

时间:2014-05-01 22:16:22

标签: google-apps-script

我需要帮助处理我编写的谷歌脚本,该脚本从谷歌表单中获取响应,并将响应电子表格中的响应转移到谷歌文档模板中。我现在编写的脚本具有创建我想要创建的Google文档所需的正确触发器,但我不知道需要添加什么才能将新文档保存在我的驱动器中创建的文件夹的文件夹中只是我的驱动器文件夹。任何帮助,将不胜感激。下面是我已经写过的脚本。


// Get template from Google Docs and name it
var docTemplate = "1NPiAbaB2wciyJUhJE4AvxZ1dCB_Uu5kf24eSxLC_BfI";
var docName = "AttrVaRepairTemp";

// When Form Gets submitted
function onFormSubmit(e) {
//Get information from form and set as variables
var timestamp = e.values[0];
var contact_name = e.values[1];
var company_name = e.values[2];
var phone_number = e.values[3];
var email_address = e.values[4];
var contact_address = e.values[5];
var item_type = e.values[6];
var manufacturer = e.values[7];
var model_number = e.values[8];
var serial_number = e.values[9];
var description = e.values[10];
var symptoms = e.values[11];
var accepted_by = e.values[12];
var warranty = e.values[13];

// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DocsList.getFileById(docTemplate)
.makeCopy('Repair Form- '+contact_name)
.getId();

// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();

// Replace place holder keys,in our google doc template
copyBody.replaceText('keytimestamp', timestamp);
copyBody.replaceText('keyacceptedby', accepted_by);
copyBody.replaceText('keycontactname', contact_name);
copyBody.replaceText('keycompanyname', company_name);
copyBody.replaceText('keycontactphone', phone_number);
copyBody.replaceText('keycontactemail', email_address);
copyBody.replaceText('keycontactaddress', contact_address);
copyBody.replaceText('keymanufacturer', manufacturer);
copyBody.replaceText('keymodel', model_number);
copyBody.replaceText('keyserial', serial_number);
copyBody.replaceText('keyitemtype', item_type);
copyBody.replaceText('keywarranty', warranty);
copyBody.replaceText('keydescription', description);
copyBody.replaceText('keysymptoms', symptoms);  

// Save and close the temporary document
copyDoc.saveAndClose();

1 个答案:

答案 0 :(得分:0)

(复制的Serge Insas从其他地方的类似问题回答)

请参阅此旧帖子:(https://stackoverflow.com/questions/13532114/google-apps-script-addtofolder-adds-to-folder-and-mydrive/13536275#13536275

代码如下:

    function moveFromRoot(){
    folder=DocsList.createFolder("MyFolder");
    var file=DocsList.createFile('File2', 'Empty');
    file.addToFolder(folder);
    file.removeFromFolder(DocsList.getRootFolder());
    }

但如答案中所述,您可以使用文件夹对象本身创建文件(folder.createFile('blabla'))