I have a Google Sheet with a bunch of numbers that get added up and totalled using the SUM feature.
The totals are in cells D2 and E2
I need a Google Apps Script that will export each of those cells individually to their own .txt files (labelled D2Total.txt & E2Total.txt) in my google drive.
I need them to overwrite themselves every time the script is run because I will be having them update constantly (every few minutes) using the triggers in G Suite.
This is probably pretty straight forward but I am lame when it comes to coding.
I tried some random code on the internet that was kind of close but would only export the entire spreadsheet.
Assuming you have CSV data in a 'A2' like this '1,2,3,4,5,6,7,8,9,10,11,12 13,14,14,16,17,18,19,20,21,22,23,24 with line delimiters '\\n' the you can use this code to create a Google Document and append it as a table or simply export it to an ascii text file.
Export to Google Document:
function exportToGDoc() {
var fn=Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yy:HH:mm") + '_Report';
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet62');//Need Sheet Name
var rg=sh.getRange(1,1);
var csv=rg.getValue();
var vA=Utilities.parseCsv(csv);
var fldr=DriveApp.getFolderById('Your Folder Id');//Need Folder Id
var dk=DocumentApp.create(fn);
var bd=dk.getBody();
bd.appendTable(vA)
dk.saveAndClose();
var fileId=dk.getId();
Drive.Files.update({"parents": [{"id": fldr.getId()}]}, dk.getId());//Need to install Drive API
}
Export to Ascii Text File:
function exportToAsciiText() {
var fn=Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yy:HH:mm") + '_Report.csv';
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet62');//Need Sheet Name
var rg=sh.getRange(1,1);//A1
var csv=rg.getValue();
var fldr=DriveApp.getFolderById('Your Folder Id');//Need Folder Id
var file=fldr.createFile(fn, csv, MimeType.PLAIN_TEXT);
}
function exportToAsciiText() {
var fn='MyReport.csv';//Your report name
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet62');//Need Sheet Name
var rg=sh.getRange(1,1);//A1
var csv=rg.getValue();
var fldr=DriveApp.getFolderById('Your Folder Id');//Need Folder Id
var files=fldr.getFilesByName(fn);
var n=0;
while(files.hasNext()) {
var file=files.next();
n++;
}
switch (n) {
case 0:
var file=fldr.createFile(fn, csv, MimeType.PLAIN_TEXT);
break;
case 1:
file.setContent(csv);
break;
default:
SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutput(Utilities.formatString('You have more than one file named %s in folder named %s.',fn,fldr.getName())), 'Error: To many files with that name.');
break;
}
}
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.