I'm importing a CSV from an email with the following code:
function RetrieveAttachment()
{
var threads = GmailApp.search("Report*")
var msgs = GmailApp.getMessagesForThreads(threads);
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var today= new Date();today.setDate(today.getDate());
var today= Utilities.formatDate(today, "GMT+1", "dd.MM.yyyy");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Import");
for(var i = 0; i < msgs.length; i++)
{
for(var j = 0; j < msgs[i].length; j++)
{
var msgdate = Utilities.formatDate(new Date(msgs[i][j].getDate()), "GMT+1", "dd.MM.yyyy");
if(msgdate == today)
{
var attachments = msgs[i][j].getAttachments();
for(var k = 0; k < attachments.length; k++)
{
var attachmentName = attachments[k].getName();
var stringValue = attachmentName.search("*");
if(stringValue > -1)
{
var attachmentData = attachments[k].getDataAsString();
var parseCsv = Utilities.parseCsv(attachmentData, ",");
sheet.clearContents();
sheet.getRange(1,1, parseCsv.length, parseCsv[0].length).setValues(parseCsv);
}
}
}
}
}
}
Usually this works just fine, but with one specific csv I get via email only this two question marks are being written in the Spreadsheet:
I checked with the debugger and apparently the whole content of the csv is being read, however not correctly parsed:
I presume this is some coding problem, however I tried with the base64Encode/base64Decode utilities without any success.
Does someone have any clue on how to solve this?
The original CSV file was UTF-16 and tab separated, I solved it specifying the encoding and different separator:
var attachmentData = attachments[k].getDataAsString("UTF-16");
var parseCsv = Utilities.parseCsv(attachmentData, "\t");
Here the new Code
function RetrieveAttachment()
{
var threads = GmailApp.search("Report*")
var msgs = GmailApp.getMessagesForThreads(threads);
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var today= new Date();today.setDate(today.getDate());
var today= Utilities.formatDate(today, "GMT+1", "dd.MM.yyyy");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Import");
for(var i = 0; i < msgs.length; i++)
{
for(var j = 0; j < msgs[i].length; j++)
{
var msgdate = Utilities.formatDate(new Date(msgs[i][j].getDate()), "GMT+1", "dd.MM.yyyy");
if(msgdate == today)
{
var attachments = msgs[i][j].getAttachments();
for(var k = 0; k < attachments.length; k++)
{
var attachmentName = attachments[k].getName();
var stringValue = attachmentName.search("*");
if(stringValue > -1)
{
var attachmentData = attachments[k].getDataAsString("UTF-16");
var parseCsv = Utilities.parseCsv(attachmentData, "\t");
sheet.clearContents();
sheet.getRange(1,1, parseCsv.length, parseCsv[0].length).setValues(parseCsv);
}
}
}
}
}
}
Related
dears
I need your support to edit this function as I need to copy data with formatting
This is the equation I working on
function copyRows()
{
var copySheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheet1');
var dataSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheet2');
var dataRange = dataSheet.getDataRange();
var dataValues = dataRange.getValues();
for(var i = 1; i < dataValues.length; i++)
{
if(dataValues[i][29] === 'done')
{
copySheet.appendRow([dataValues[i][0],
dataValues[i][1],
dataValues[i][2],
dataValues[i][3],
dataValues[i][4]]);
}
}
for(var i = 1; i < dataValues.length; i++)
{
if(dataValues[i][29] === 'done')
{
var clearRow = i+1;
dataSheet.getRange('A' + clearRow + ':AC' + clearRow).clear();
}
I have a sheet named form responses which gets input from google
forms,Then I created sheets with names from Column C which is
employee code and append all the respective rows from form responses
to the newly created sheets.
Now I have sheets with name as employee code ( which is in column c of the sheet). Then I want to create secondary sheets from this master sheet(sheets with name as employee code) whose name is using the column E value as attached to the Employee code.for eg : if Emp code is 10003825 and value in column E is ASR, the sheet name should be 10001515ASR.
In Column E there are only 4 values ADC,ASR ACC,RSR. let the master sheet(sheets with name as employee code) retain ADC rows always while when other rows containing ASR,ACC,RSR comes append the respective row to the newly created sheet(ie if ASR the row goes to 10001515ASR).
I have shown my present sheet and expected output
The code I developed is shown below:
function switchSheet(){
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheets = activeSpreadsheet.getSheets();
for (var i = 1; i < sheets.length; i++) {
var p = sheets[i].getSheetName();
sheetCreateChannel(p);
}
}
function sheetCreateChannel(ss){
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = activeSpreadsheet.getSheetByName(ss);
var getNames = sheet1.getDataRange().getValues();
getNames.shift();
var sheets = activeSpreadsheet.getSheets();
var sheetsObj = {};
for (var i = 0; i < sheets.length; i++) {
sheetsObj[sheets[i].getSheetName()] = sheets[i];
for (var i = 0; i < getNames.length; i++) {
var r = getNames[i];
if (r[4].toString()== "ASR"&& !((ss+(r[4])) in sheetsObj)) {
var newSheet = activeSpreadsheet.insertSheet(ss + "ASR");
sheetsObj[ss+"ASR"] = newSheet;
}
else if (r[4].toString()== "ACC"&&!((ss+(r[4])) in sheetsObj)) {
var newSheet = activeSpreadsheet.insertSheet(ss + "ACC");
sheetsObj[ss+"ACC"] = newSheet;
}
else if (r[4].toString()== "RSR"&& !((ss+(r[4])) in sheetsObj)) {
var newSheet = activeSpreadsheet.insertSheet(ss + "RSR");
sheetsObj[ss+"RSR"] = newSheet;
}
else {
break;
}
}
}
var deleteRows = 0;
for (var i = 0; i < getNames.length; i++) {
var r = getNames[i];
if (r[4].toString() != "") {
if ((ss+(r[4])) in sheetsObj) {
var Q =ss+(r[4]);
sheetsObj[Q].appendRow(r);
}
deleteRows = i + 2;
}
}
sheet1.getRange(2, 1, deleteRows - 1, sheet1.getLastColumn()).clearContent();
}
This code works fine for my above issue
function sheetCreate(){
var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = activeSpreadsheet.getSheetByName("Form Responses 1");
var getNames = sheet1.getDataRange().getValues();
getNames.shift();
var sheets = activeSpreadsheet.getSheets();
var sheetsObj = {};
for (var i = 0; i < sheets.length; i++) {
sheetsObj[sheets[i].getSheetName()] = sheets[i];
}
var deleteRows = 0;
for (var i = 0; i < getNames.length; i++) {
var r = getNames[i];
var N=r[2].toString()+r[4].toString();
if (N != "") {
if (N in sheetsObj) {
sheetsObj[N].appendRow(r);
} else {
var newSheet = activeSpreadsheet.insertSheet((N));
var cell1 = newSheet.getRange("A1").setValue("Timestamp");
var cell2 = newSheet.getRange("B1").setValue("Score");
var cell3 = newSheet.getRange("C1").setValue("Employee ID");
var cell4 = newSheet.getRange("D1").setValue("Name of the ATCO");
var cell5 = newSheet.getRange("E1").setValue("The channel on which duty is performed ");
var cell6 = newSheet.getRange("F1").setValue("DATE on which duty is performed");
var cell7 = newSheet.getRange("G1").setValue("Total duration of duty done");
newSheet.appendRow(r);
sheetsObj[N] = newSheet;
}
deleteRows = i + 2;
}
}
sheet1.getRange(2, 1, deleteRows - 1, sheet1.getLastColumn()).clearContent();
}
Below is a script written by Jordan Rhea and provided via github.
I am trying to follow its logic so that I can understand how to approach automatically populating a form with questions that have conditional logic flowing to different sections of a Google Form.
The code is breaking at line 35 "ReferenceError: form is not defined (line 35, file "RosterMaker")
Line 35 = var classSection = form.addPageBreakItem().setTitle(className).setGoToPage(FormApp.PageNavigationType.SUBMIT);
I'm sure this is a simple error, but I cannot figure it out.
var ssID = "something";
var formID = "something"
//SpreadsheetApp.openById(ssID).getSheetByName("db_type");
function rosterMaker() {
//spreadsheet id of the rosters
var SHEET_ID = SpreadsheetApp.getActive();
var ss = SpreadsheetApp.openById(ssID);
var form = FormApp.openById((formID));
//get only the sheets with 'Roster' in the title
var sheets = ss.getSheets()
.filter(function(sheet) {return sheet.getName().match(/Roster/gi);});
//add multiple choice item
var classSelect = form.addMultipleChoiceItem()
.setTitle('Choose a class');
Logger.log(classSelect);
//get the class choices for the multiple choice item
var classChoices = getClasses(sheets);
//assign the choices to the classSelect variable
classSelect.setChoices(classChoices);
}
function getClasses(sheets) {
var classChoices = [];
for(var i = 0; i < sheets.length; i++) {
var className = sheets[i].getName();
var classSection = form.addPageBreakItem()
.setTitle(className)
.setGoToPage(FormApp.PageNavigationType.SUBMIT);
var students = getStudents(sheets[i]);
var studentSelect = form.addCheckboxItem()
.setTitle(className + ' absent')
.setHelpText('Select the students who are absent from this class');
var studentChoices = [];
for(var j = 0; j < students.length; j++) {
studentChoices.push(studentSelect.createChoice(students[j]));
}
studentSelect.setChoices(studentChoices);
classChoices.push(classSelect.createChoice(className, classSection));
}
return classChoices;
}
function getStudents(sheet) {
var studentValues = sheet.getDataRange().getValues();
var students = [];
for(var i = 1; i < studentValues.length; i++) {
students.push(studentValues[i].join(' '));
}
return students;
}
Test this:
var ssID = "something";
var formID = "something";
function rosterMaker() {
var SHEET_ID = SpreadsheetApp.getActive();
var ss = SpreadsheetApp.openById(ssID);
var form = FormApp.openById((formID));
var sheets = ss.getSheets().filter(function(sheet) {return sheet.getName().match(/Roster/gi);});
var classSelect = form.addMultipleChoiceItem().setTitle('Choose a class');
var classChoices = getClasses(sheets,form);//modified
classSelect.setChoices(classChoices);
}
function getClasses(sheets,form) {//modified
var classChoices = [];
for(var i = 0; i < sheets.length; i++) {
var className = sheets[i].getName();
var classSection = form.addPageBreakItem().setTitle(className).setGoToPage(FormApp.PageNavigationType.SUBMIT);
var students = getStudents(sheets[i]);
var studentSelect = form.addCheckboxItem().setTitle(className + ' absent').setHelpText('Select the students who are absent from this class');
var studentChoices = [];
for(var j = 0; j < students.length; j++) {
studentChoices.push(studentSelect.createChoice(students[j]));
}
studentSelect.setChoices(studentChoices);
classChoices.push(classSelect.createChoice(className, classSection));
}
return classChoices;
}
function getStudents(sheet) {
var studentValues = sheet.getDataRange().getValues();
var students = [];
for(var i = 1; i < studentValues.length; i++) {
students.push(studentValues[i].join(' '));
}
return students;
}
I've created the following code to run a manual script to change the day in multiple instances within a Google Slides presentation. If the day is Monday, the script changes it to Thursday. If it is Thursday, the other script changes it to Monday.
function futurebriefthurs() {
var currdate = new Date();
var daystochange = 3;
var newbriefday = new Date(currdate.getFullYear(), currdate.getMonth(), currdate.getDate() + daystochange);
var NextBrief = Utilities.formatDate(new Date(newbriefday), "GMT-7", "EEEEE");
var pattern = "\\b\\d{1,2}/\\d{1,2}/\\d{4}\\b";
var slides = SlidesApp.getActivePresentation().getSlides();
var slidesLength = slides.length;
for (var i = 0; i < slidesLength; i++) {
var shapes = slides[i].getShapes();
var shapesLength = shapes.length;
for (var j = 0; j < shapesLength; j++) {
if (shapes[j].getDescription() == "$NextBrief") {
var textRange = shapes[j].getText();
textRange.clear();
textRange.insertText(0, NextBrief);
}
}
}
}
function futurebriefmon() {
var currdate = new Date();
var daystochange = 4;
var newbriefday = new Date(currdate.getFullYear(), currdate.getMonth(), currdate.getDate() + daystochange);
var NextBrief = Utilities.formatDate(new Date(newbriefday), "GMT-7", "EEEEE");
var pattern = "\\b\\d{1,2}/\\d{1,2}/\\d{4}\\b";
var slides = SlidesApp.getActivePresentation().getSlides();
var slidesLength = slides.length;
for (var i = 0; i < slidesLength; i++) {
var shapes = slides[i].getShapes();
var shapesLength = shapes.length;
for (var j = 0; j < shapesLength; j++) {
if (shapes[j].getDescription() == "$NextBrief") {
var textRange = shapes[j].getText();
textRange.clear();
textRange.insertText(0, NextBrief);
}
}
}
}
What I would like to do is have a script that runs upon opening the presentation that checks the current day and makes the same changes automatically. We use this presentation on scheduled days so the need to change the day to other than Monday and Thursday is minimal.
At some point, I would like to be able to have the changed based on Google Calendar entries, but that is for another day. :-)
I think something like this will work.
You just need to run createOpenTrigger() when you install the code. I assumed everything else in the code was working.
function createOpenTrigger(){
var triggers=ScriptApp.getProjectTriggers();
var tA=[];
for(var i=0;i<triggers.length;i++){tA.push(triggers[i].getHandlerFunction());}
if(tA.indexOf('futureBrief')!=-1){
ScriptApp.newTrigger('futureBrief').forSpreadsheet(sheet).onOpen().create();
}
}
function futureBrief() {
var currdate = new Date();
var dtcA=[0,4,0,0,3,0,0];
var daystochange=dtcA[currdate.getDay()];
if(daystochange>0){
var newbriefday = new Date(currdate.getFullYear(), currdate.getMonth(), currdate.getDate() + daystochange);
var NextBrief = Utilities.formatDate(new Date(newbriefday), "GMT-7", "EEEEE");
var pattern = "\\b\\d{1,2}/\\d{1,2}/\\d{4}\\b";
var slides = SlidesApp.getActivePresentation().getSlides();
var slidesLength = slides.length;
for (var i = 0; i < slidesLength; i++) {
var shapes = slides[i].getShapes();
var shapesLength = shapes.length;
for (var j = 0; j < shapesLength; j++) {
if (shapes[j].getDescription() == "$NextBrief") {
var textRange = shapes[j].getText();
textRange.clear();
textRange.insertText(0, NextBrief);
}
}
}
}
}
This is my script. I need to populate the values in I2:I range when the cell value of C2 changes. I have created an array_temp but these are not getting pasted in I2. Can anybody please help me throw some light on it?
function onEdit(e) {
var spreadsheet = SpreadsheetApp.getActive();
var ss = e.getSheetByName('UserInterface');
var count = ss.getRange("H2").setFormula('=countif(TalukaName!B:B,UserInterface!C2)').getValue();
var DistrictName = ss.getRange('C2').getValue();
var matchindex = ss.getRange('H3').setFormula('=match(C2,TalukaName!B:B,0)').getValue();
var indexvalue = ss.getRange('H4').setFormula('=index(TalukaName!B:B,H3)').getValue();
var array = [] ;
ss.getRange('F2').clearcontent;
var ssTaluka= e.getSheetByName('TalukaName');
var range = ssTaluka.getDataRange();
var data = range.getValues();
for (var i = 0; i < count; i++) {
array[i]= data[i + matchindex - 1][0];
}
var array_temp = [];
for (var j = 0; j < array.length; j++) {
array_temp.push([
array[j]
])
}
ss.getRange('I2:I').clearContent();
ss.getRange('I2:I'+ (count+1)).setValues(array_temp) ;
}