I am creating a script with google appscript to
read files in a folder
parse the name of the file in the folder
check if that name already exists in the sheet
if not add that to the list
if it does exist then check another column to see if an email is sent
if yes do nothing if no send the email.
I have tried
index of
,for loop to iterate over range.getValues()
None of them work properly as expected.
The data is of length 3.
function myFunction() {
getFileNameFromFolders('1TcR5oUKwH9hUG9xHBA6HuXQOr40etS5z');
}
function getFileNameFromFolders(folderID) {
var folder = DriveApp.getFolderById(folderID);
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var fileName = file.getName();
var agentDetails = fileName.split("-");
var agentID = agentDetails[0];
var fileType = agentDetails[1];
var fileUrl = file.getUrl();
var fileDate = agentDetails[2];
locateAgent(agentID, fileType, fileDate, fileUrl, fileName);
}
}
function locateAgent(agentID, fileType, fileDate, url, uniqueKey) {
Logger.log('locating ' + uniqueKey);
var spreadSheet = SpreadsheetApp.openByUrl(SpreadsheetApp.getActiveSpreadsheet().getUrl());
var sheet = spreadSheet.getSheets()[0];
var range = sheet.getRange(2, 1, sheet.getLastRow() - 1, 6)
var data = range.getValues();
for (var i in data) {
if (data[i][5] == uniqueKey) {
Logger.log('yes');
break;
}
else { Logger.log('no');
var newRange = sheet.appendRow([agentID,fileType, fileDate, url, 'r', uniqueKey]);}
}
}
function sendEmails(email, fileUrl) {
var asPDF = DriveApp.getFileById(getIdFromUrl(fileUrl));
MailApp.sendEmail(email, 'test-email-with-agent-stuff-thing-i-dont-know-the-name', 'you should recieve a file named AID-2 as you are registered as 2', {
name: 'Automatic Emailer Script from DOER',
attachments: asPDF.getAs(MimeType.PDF)
});
}
function getIdFromUrl(url) {
return url.match(/[-\w]{25,}$/);
}
The loop adds to the list even though it exists. I may be getting the concept. If you have any other way I can do this, I would highly appreciate it.
You can implement a boolean variable which will be set to true if uniqueKey exists alredy
Modify your code as following:
function locateAgent(agentID, fileType, fileDate, url, uniqueKey) {
Logger.log('locating ' + uniqueKey);
var spreadSheet = SpreadsheetApp.openByUrl(SpreadsheetApp.getActiveSpreadsheet().getUrl());
var sheet = spreadSheet.getSheets()[0];
var range = sheet.getRange(2, 1, sheet.getLastRow()-1, 6)
var data = range.getValues();
var exists=false;
for (var i in data) {
if (data[i][5] == uniqueKey){
exists=true;
var row=i;
break;
}
}
if(exists==false){
Logger.log('it does not exist yet');
var insertRange = sheet.getRange(sheet.getLastRow()+1, 1, 1, 6);
//adapt according to your needs:
insertRange.setValues([[agentID],[fileType],[fileDate],[url],[],[uniqueKey]]);
}else{
//implement here your statement to check status column, e.g.:
if(data[row][status column]!="Sent"){
sendEmails(...);
}
}
}
function tree1(){
var secret =SpreadsheetApp.openById('My Database Secret') ;
var sheet = secret.getSheets()[0];
var data = sheet.getDataRange().getValues();
var dataToImport = {};
for(var i = 1; i < data.length; i++) {
var admin = data[i][0];
var email = data[i][1];
var name = data[i][2];
var subject = data[i][3];
var year = data[i][4];
var testone = data[i][5];
var testtwo = data[i][6];
var testthree = data[i][7];
var average = data[i][8];
var progress = data[i][9];
dataToImport[email] = {
name:name,
emailAddress:email,
adminNo:admin,
Subject:subject,
Year:year,
testOne:testone,
testTwo:testtwo,
testThree:testthree,
Average:average,
Progress:progress
};
}
var firebaseUrl = "My Database URL";
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
base.setData("", dataToImport);
}
I am attempting to use a Google Sheet App script to sync basic data for my users, using their email address as a reference.
So that when a user logs in with their email they can access test scores that the admin has.
The Database structure I would like is fairly simple that the three scores and other details are a child of the email, as you can see above.
However every time I attempt to complete the script I receive a Bad Value error.
Any ideas as to how I can remedy this problem.
Thank you in advance.
Bellow is my code
public async void TestLoops(long device, double date1, double date2)
{
var connectionString = "mongodb://localhost.:27017";//Connection string
MongoClientSettings settings1 = MongoClientSettings.FromUrl(new MongoUrl(connectionString));// Set Url to the MongoClient
//MongoS
// MongoClient mongoClient = new MongoClient(settings1);
var client = new MongoClient(new MongoClientSettings
{
Server = new MongoServerAddress("connectionString"),
ClusterConfigurator = builder =>
{
builder.ConfigureCluster(settings => settings.With(serverSelectionTimeout: TimeSpan.FromSeconds(1000)));
}
});
//Server s = new MongoServerAddress(connectionString);
var db = client.GetDatabase("tracking");//Specifing the Database name
var user = db.GetCollection<BsonDocument>("location");//Specifing t
var builder1 = Builders<BsonDocument>.Filter;
// var builder1 = Builders<BsonDocument>.Filter;
//int
var filt = builder1.Eq("device", device) & builder1.Gte("timestamp", date1) & builder1.Lte("timestamp", date2);
//var filter = builder1.Eq("device", 358740050124519);
var data = user.Find(filt).Count();
lblmsg.Text = data.ToString();
}
when i write the var data = user.Find(filt); it works fine
You can do
var collection = database.GetCollection<Type>("DBName");
var cursor = collection.Find(Query.EQ("FieldToMatch" : "ValueToMatch"));
var count = cursor.Count();
another approach
db.collection.CountAsync(); // you can pass new BsonDocument() or a condition to countAsync based on your requirement.
I there a way to display specific collection files and URL in a spreadsheet?
I have already tried running a basic DocList Search script but I need something to be more direct. I would need the script to display File Name, Collections It belongs to, and URL.
The end goal of this project is to create a Google site that allows users to click a Image link launching a simple "Copy Function" this copy function will create a copy of that document for the user in there individual drive. However we will be doing this on a mass scale of over 1,000 documents. So pulling the information and having it more organized would be alot easier then copying the URL section out of each document and then pasting it into the scripts functions.
here is a script I wrote quite a while ago that does (a bit more than ) what you want... it shows the IDs but you can easily change it to show the urls instead.
// G. Variables
var sh = SpreadsheetApp.getActiveSheet();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var lastrow = ss.getLastRow();
//
//
//
function onOpen() {
var menuEntries = [ {name: "generic doclist", functionName: "gendoclisttest"},
{name: "categorized list(spreadsheet/docs)", functionName: "doclistcat"},
{name: "Search DocList", functionName: "searchUI"},
];
ss.addMenu("Utilities", menuEntries);//
}
//
// Build a simple UI to enter search item and show results + activate result's row
function searchUI() {
var app = UiApp.createApplication().setHeight(130).setWidth(400);
app.setTitle("Search by name or folder name");
var panel = app.createVerticalPanel();
var txtBox = app.createTextBox().setFocus(true).setWidth("180");
var label=app.createLabel(" Eléments à rechercher :")
var label=app.createLabel(" Item to search for :")
panel.add(label);
txtBox.setId("item").setName("item");
var label0=app.createLabel("Row").setWidth("40");
var label1=app.createLabel("Doc Name").setWidth("180");
var label2=app.createLabel("Doc ID").setWidth("180");
var hpanel = app.createHorizontalPanel();
hpanel.add(label0).add(label1).add(label2);
//
var txt0=app.createTextBox().setId("lab0").setName("0").setWidth("40");
var txt1=app.createTextBox().setId("lab1").setName("txt1").setWidth("180");
var txt2=app.createTextBox().setId("lab2").setName("txt2").setWidth("180");
var hpanel2 = app.createHorizontalPanel();
hpanel2.add(txt0).add(txt1).add(txt2);
var hidden = app.createHidden().setName("hidden").setId("hidden");
var subbtn = app.createButton("next ?").setId("next").setWidth("250");
panel.add(txtBox);
panel.add(subbtn);
panel.add(hidden);
panel.add(hpanel);
panel.add(hpanel2);
var keyHandler = app.createServerHandler("click");
txtBox.addKeyUpHandler(keyHandler)
keyHandler.addCallbackElement(panel);
//
var submitHandler = app.createServerHandler("next");
subbtn.addClickHandler(submitHandler);
submitHandler.addCallbackElement(panel);
//
app.add(panel);
ss.show(app);
}
//
function click(e){
var row=ss.getActiveRange().getRowIndex();
var app = UiApp.getActiveApplication();
var txtBox = app.getElementById("item");
var subbtn = app.getElementById("next").setText("next ?")
var txt0=app.getElementById("lab0").setText('--');
var txt1=app.getElementById("lab1").setText('no match').setStyleAttribute("background", "white");// default value to start with
var txt2=app.getElementById("lab2").setText('');
var item=e.parameter.item.toLowerCase(); // item to search for
var hidden=app.getElementById("hidden")
var data = sh.getRange(2,1,lastrow,8).getValues();// get the 8 columns of data
for(nn=0;nn<data.length;++nn){ ;// iterate trough
if(data[nn].toString().toLowerCase().match(item.toString())==item.toString()&&item!=''){;// if a match is found in one of the 3 fields, break the loop and show results
var datarow=data[nn]
for(cc=0;cc<datarow.length;++cc){
if(datarow[cc].toString().toLowerCase().match(item.toString())==item.toString()&&item!=''){break}
}
var idx=cc
txt0.setText(nn+2);
txt1.setText(data[nn][idx]).setStyleAttribute("background", "cyan");
txt2.setText(data[nn][idx+1]);
sh.getRange(nn+2,idx+1).activate();
subbtn.setText("found '"+item+"' in row "+Number(nn+2)+", next ?");
hidden.setValue(nn.toString())
break
}
}
return app ;// update UI
}
function next(e){
var row=ss.getActiveRange().getRowIndex();
var app = UiApp.getActiveApplication();
var txtBox = app.getElementById("item");
var subbtn = app.getElementById("next").setText("no other match")
var hidden=app.getElementById("hidden");
var start=Number(e.parameter.hidden)+1;//returns the last search index stored in the UI
var item=e.parameter.item.toLowerCase(); // item to search for
var txt0=app.getElementById("lab0");
var txt1=app.getElementById("lab1").setStyleAttribute("background", "yellow");
var txt2=app.getElementById("lab2");
var data = sh.getRange(2,1,lastrow,8).getValues();// get the 3 columns of data
for(nn=start;nn<data.length;++nn){ ;// iterate trough
if(data[nn].toString().toLowerCase().match(item.toString())==item.toString()&&item!=''){;// if a match is found in one of the 3 fields, break the loop and show results
var datarow=data[nn]
for(cc=0;cc<datarow.length;++cc){
if(datarow[cc].toString().toLowerCase().match(item.toString())==item.toString()&&item!=''){break}
}
var idx=cc
txt0.setText(nn+2);
txt1.setText(data[nn][idx]).setStyleAttribute("background", "cyan");
txt2.setText(data[nn][idx+1]);
sh.getRange(nn+2,idx+1).activate();
subbtn.setText("found '"+item+"' in row "+Number(nn+2)+", next ?");
hidden.setValue(nn.toString())
break
}
}
return app ;// update UI
}
//
function gendoclisttest(){
sh.getRange(1,1).setValue('.');// usefull to allow for 'clear' if page is empty
sh.getRange(1,1,ss.getLastRow(),ss.getLastColumn()).clear().setWrap(false).setBorder(false,false,false,false,false,false);// clears whole sheet
var doclist=new Array();
var folders=DocsList.getFolders()
for(ff=0;ff<folders.length;++ff){
doclist=folders[ff].getFiles(0,2000)
var names = new Array();
for (nn=0;nn<doclist.length;++nn){
names.push([doclist[nn].getName(),doclist[nn].getId()]);
}
if (names.length>0){
names.sort();
var row=ss.getLastRow()+1;
sh.getRange(row,1,1,3).setValues([["Folders","Generic Doc Names","ID"]]).setBorder(false,true,true,true,true,true).setBackgroundColor("#dddddd");
sh.getRange(row+1,1).setValue(folders[ff].getName())
sh.getRange(row+1,2,names.length,2).setValues(names);
}
}
doclist=DocsList.getRootFolder().getFiles(0,2000)
var names = new Array();
for (nn=0;nn<doclist.length;++nn){
names.push([doclist[nn].getName(),doclist[nn].getId()]);
}
if (names.length>0){
names.sort();
var row=ss.getLastRow()+1;
sh.getRange(row,1,1,3).setValues([["Root","Generic Doc Names","ID"]]).setBorder(false,true,true,true,true,true).setBackgroundColor("#dddddd");
sh.getRange(row+1,2,names.length,2).setValues(names);
}
}
//
function doclistcat(){
var doclist=new Array();
var folders=DocsList.getFolders()
var zz=0;var nn=0
for(ff=0;ff<folders.length;++ff){
doclist=folders[ff].getFilesByType("spreadsheet",0,2000);
var names = new Array();
for (nn=0;nn<doclist.length;++nn){
names.push([doclist[nn].getName(),doclist[nn].getId()]);
}
if(names.length>0){
names.sort();
zz=zz+nn
var row=zz-nn+1
sh.getRange(row,4,1,3).setValues([["Folders","Spreadsheet Names","ID"]]).setBorder(true,true,true,true,true,true).setBackgroundColor("#dddddd");
sh.getRange(row+1,4).setValue(folders[ff].getName()).setB
sh.getRange(row+1,5,names.length,2).setValues(names);
}
}
doclist=DocsList.getRootFolder().getFilesByType("spreadsheet",0,2000);
var names = new Array();
for (nn=0;nn<doclist.length;++nn){
names.push([doclist[nn].getName(),doclist[nn].getId()]);
}
if(names.length>0){
names.sort();
zz=zz+nn
var row=zz-nn+1
sh.getRange(row,4,1,3).setValues([["Root","Spreadsheet Names","ID"]]).setBorder(true,true,true,true,true,true).setBackgroundColor("#dddddd");
sh.getRange(row+1,5,names.length,2).setValues(names);
}
//
var zz=0;var nn=0
for(ff=0;ff<folders.length;++ff){
doclist=folders[ff].getFilesByType("document",0,2000);
var names = new Array();
for (nn=0;nn<doclist.length;++nn){
names.push([doclist[nn].getName(),doclist[nn].getId()]);
}
if(names.length>0){
names.sort();
zz=zz+nn
var row=zz-nn+1
sh.getRange(row,7,1,3).setValues([["Folders","Text Document Names","ID"]]).setBorder(true,true,true,true,true,true).setBackgroundColor("#dddddd");
sh.getRange(row+1,7).setValue(folders[ff].getName()).setB
sh.getRange(row+1,8,names.length,2).setValues(names);
}
}
doclist=DocsList.getRootFolder().getFilesByType("document",0,2000);
var names = new Array();
for (nn=0;nn<doclist.length;++nn){
names.push([doclist[nn].getName(),doclist[nn].getId()]);
}
if(names.length>0){
names.sort();
zz=zz+nn
var row=zz-nn+1
sh.getRange(row,7,1,3).setValues([["Root","document Names","ID"]]).setBorder(true,true,true,true,true,true).setBackgroundColor("#dddddd");
sh.getRange(row+1,8,names.length,2).setValues(names);
}
}
//
//eof
i'm loading several sound files, and want to error check each load. however, instead programming each one with their own complete/error functions, i would like them to all use the same complete/error handler functions.
a successfully loaded sound should create a new sound channel variable, while an unsuccessfully loaded sound will produce a simple trace with the name of the sound that failed to load. however, in order to do this, i need to dynamically create variables, which i haven't yet figured out how to do.
here's my code for my complete and error functions:
function soundLoadedOK(e:Event):void
{
//EX: Sound named "explosion" will create Sound Channel named "explosionChannel"
var String(e.currentTarget.name + "Channel"):SoundChannel = new SoundChannel();
}
function soundLoadFailed(e:IOErrorEvent):void
{
trace("Failed To Load Sound:" + e.currentTarget.name);
}
-=-=-=-=-=-=-=-=-
UPDATED (RE: viatropos)
-=-=-=-=-=-=-=-=-
can not find the error.
TypeError: Error #1009: Cannot access a property or method of a null object reference. at lesson12_start_fla::MainTimeline/loadSounds() at lesson12_start_fla::MainTimeline/frame1():
//Sounds
var soundByName:Object = {};
var channelByName:Object = {};
var soundName:String;
var channelName:String;
loadSounds();
function loadSounds():void
{
var files:Array = ["robotArm.mp3", "click.mp3"];
var i:int = 0;
var n:int = files.length;
for (i; i < n; i++)
{
soundName = files[i];
soundByName[soundName] = new Sound();
soundByName[soundName].addEventListener(Event.COMPLETE, sound_completeHandler);
soundByName[soundName].addEventListener(IOErrorEvent.IO_ERROR, sound_ioErrorHandler);
soundByName[soundName].load(new URLRequest(soundName));
}
}
function sound_completeHandler(event:Event):void
{
channelName = event.currentTarget.id3.songName;
channelByName[channelName] = new SoundChannel();
}
function sound_ioErrorHandler(event:IOErrorEvent):void
{
trace("Failed To Load Sound:" + event.currentTarget.name);
}
You can do this a few ways:
Storing your SoundChannels in an Array. Good if you care about order or you don't care about getting them by name.
Storing SoundChannels by any name in an Object. Good if you want to easily be able to get the by name. Note, the Object class can only store keys ({key:value} or object[key] = value) that are Strings. If you need Objects as keys, use flash.utils.Dictionary, it's a glorified hash.
Here's an example demonstrating using an Array vs. an Object.
var channels:Array = [];
// instead of creating a ton of properties like
// propA propB propC
// just create one property and have it keep those values
var channelsByName:Object = {};
function loadSounds():void
{
var files:Array = ["soundA.mp3", "soundB.mp3", "soundC.mp3"];
var sound:Sound;
var soundChannel:SoundChannel;
var i:int = 0;
var n:int = files.length;
for (i; i < n; i++)
{
sound = new Sound();
sound.addEventListener(Event.COMPLETE, sound_completeHandler);
sound.addEventListener(IOErrorEvent.IO_ERROR, sound_ioErrorHandler);
sound.load(files[i]);
}
}
function sound_completeHandler(event:Event):void
{
// option A
var channelName:String = event.currentTarget.id3.songName;
// if you want to be able to get them by name
channelsByName[channelName] = new SoundChannel();
// optionB
// if you just need to keep track of all of them,
// and don't care about the name specifically
channels.push(new SoundChannel())
}
function sound_ioErrorHandler(event:IOErrorEvent):void
{
trace("Failed To Load Sound:" + event.currentTarget.name);
}
Let me know if that works out.
//Load Sounds
var soundDictionary:Dictionary = new Dictionary();
var soundByName:Object = new Object();
var channelByName:Object = new Object();
loadSounds();
function loadSounds():void
{
var files:Array = ["robotArm.mp3", "click.mp3"]; //etc.
for (var i:int = 0; i < files.length; i++)
{
var soundName:String = files[i];
var sound:Sound=new Sound();
soundDictionary[sound] = soundName;
soundByName[soundName] = sound;
sound.addEventListener(Event.COMPLETE, sound_completeHandler);
sound.addEventListener(IOErrorEvent.IO_ERROR, sound_ioErrorHandler);
sound.load(new URLRequest(soundName));
}
}
function sound_completeHandler(e:Event):void
{
var soundName:String=soundDictionary[e.currentTarget];
channelByName[soundName] = new SoundChannel();
}
function sound_ioErrorHandler(e:IOErrorEvent):void
{
trace("Failed To Load Sound:" + soundDictionary[e.currentTarget]);
}
//Play Sound
channelByName["robotArm.mp3"] = soundByName["robotArm.mp3"].play();
//Stop Sound
channelByName["robotArm.mp3"].stop();