Using Drive.Properties.update to update or add a file property - google-apps-script

I have enabled the Advanced Drive Service in an apps script. For a file in Google Drive, I need to set metadata properties that may already exist, so I am trying to use the update method of Drive.Properties. This method is supposed to "Update a property or add it if it doesn't exist." (See Properties: update.)
The following code fails silently (with nothing logged to the Logger and no properties added to the file). However, if I step through these same lines, I do see the catch block executed every time.
var fileId = '1jgoihblahblahblah' /* an existing file id */;
var property = {key: 'TestKey', value: 'TestValue', visibility: 'PUBLIC'}
try {
Drive.Properties.update(property, fileId, "TestKey");
} catch (e) {
Logger.log(e);
}
If I replace the call to update with this line:
Drive.Properties.insert(property, fileId);
... then the code works every time. The properties are written and are returned by calling Drive.Properties.list(fileId).
Testing the same values using the API explorer (with real values or the "TestKey" and "TestValue" examples above) always works correctly, so I don't think it's simply a matter of providing bad data. UPDATE: The API explorer is now returning an error "Property keys may only contain letters, numbers, and the characters .!#$%^&*()-_/" no matter what values I pass in. However, there are obviously no invalid characters in key names like "TestKey" and "Link" (which is one of the actual keys I am using).
1) How can I troubleshoot the error from within the Script editor? I can't yet figure out how to retrieve the error text, since update fails silently even without the try/catch.
2) I am using the syntax specified by the autocomplete prompt, since there's not actually any documentation for Apps Script syntax for advanced service.
Is that syntax correct? If so, am I doing something wrong that I'm not seeing?

This problem is irrelevant because despite its name, Drive.Properties.insert does the same thing.
See https://issuetracker.google.com/issues/36759642

Related

UE5 - Property Metadata “EditCondition” & “EditConditionHides” Not Working When Added Via IDetailCustomization

I’ve written a small editor plugin that lets you directly edit EditCondition & EditConditionHides property metadata entries right from the editor. At first glance this seems to working - manually checking the properties metadata array, I can see the values updating and saving as expected.
However, the editor doesn’t seem to be evaluating the edit condition when values change. For example, using my custom editor to set the following condition:
Gives me the following results when changing values in the editor (in this case an editor for a custom data asset):
Notice the second example, the “Duration” field should now be enabled an editable, but it stays disabled. Am I missing something here? From what I understand adding an EditCondition to the properties metadata (i.e. meta=(EditCondition="Type == E_BuffType::Duration")) should be enough get the editor to update the way I’m expecting.

Google Script XML Cannot Call Method of Null

I have created a script which connects to an API. The script successfully parses the data and creates the required outputs. My script iterates through the record IDs to extract data for each record ID.
The existence of a child node does not exist for every parent. Thus where the parent has a child node my script operates perfectly.
var root2 = root[i].getChild('Assigned').getChildren('Staff');
However, when I encounter a parent ID where the child node does not exist I get this error. IE in the source data the client has not assigned staff to this record and thus the data node staff does not exist.
TypeError: Cannot call method "getChildren" of null.
How do I handle this error and ignore and let the code continue to run when this occurs? I have tried
if(root[i].getChild('Assigned').getChildren('Staff') != '') {
Code
}
But this still breaks the code and I get the error.
I think that the reason of the issue is that root[i].getChild('Assigned') is null, and in your script, even if Staff is not included in the parent (Assigned), no error occurs. In order to remove this issue, how about checking whether root[i] has the child of Assigned? Please try the following sample script.
Sample script:
if (root[i].getChildren().some(function(e) {return e.getName() == 'Assigned'})) {
// do something
}
In this script, it checks whether root[i] has the child of Assigned, and when the child is found, this if becomes true. So if root[i] doesn't have the child of Assigned, if becomes false, and do something is not run.
Note:
I prepared this script from the situation of your question. But I'm not sure about the actual XML data. So if this script was not what you want, can you provide the XML data or the sample data which can replicate your situation? By such sample data, I would like to modify this.
Reference:
getName()
Array.prototype.some()

StudentSubmissions.Patch UpdateMask Error

Trying to use the StudentSubmissions.Patch part of the Classroom API in Google Apps Script and keeping running across this error
updateMask: updateMask may only contain "draftGrade" or
"assignedGrade"
Here is my code for that particular section:
var studentSubmission = {'draft_grade':'88'}
var patchC = Classroom.Courses.CourseWork.StudentSubmissions.patch(studentSubmission, courseId, cwId, submissionId);
There is clearly something wrong with the way I am passing the StudentSubmission Resource parameter, but I can't figure out why...
This is clearly the documentation I am referring to - https://developers.google.com/classroom/reference/rest/v1/courses.courseWork.studentSubmissions/patch
UPDATE
I was able to change the code a bit to reflect what you both were saying. Obviously, I didn't use exactly what you both said because KENdi's example is in Python and Ein2012, it would error out on the var patchC = Classroom... line.
I changed some things that now look like this:
var studentSubmission = {'draftGrade':'88'}
var extra = {'updateMask':'draftGrade'};
var patchC = Classroom.Courses.CourseWork.StudentSubmissions.patch(studentSubmission, courseId, cwId, submissionId, extra);
But now I get a different error "#ProjectPermissionDenied The Developer Console project is not permitted to make this request". So, now I'm unsure if that formatting is correct and there is some Developer Console situation I haven't resolved (although feel as though I'm correct), or that new formatting is wrong and I'm just getting the wild permission error.
I saw this Similar Error but what if the course work was one created normally through classroom and not via a script? Ahh.
specify update mask fields and later execute and also specify names as instructed in the documentation ("draftGrade","assignedGrade")
var studentSubmission = {'draftGrade':'88'}
var patchC = Classroom.Courses.CourseWork.StudentSubmissions.patch(studentSubmission, courseId, cwId, submissionId);
patchC.UpdateMask = "draftGrade";
var response = submisionObj.Execute();
From your error, it is stated that the updateMask must only contain "draftGrade" or "assignedGrade". So from your code, you need only that two value for the updateMask.
The updateMask identifies which fields on the student submission to update. This field is required to do an update. The update fails if invalid fields are specified
From this documentation, The StudentSubmission resource has two fields to store grades: assignedGrade, which is the grade reported to students, and draftGrade, which is a tentative grade visible only to teachers. These fields are updated using courses.courseWork.studentSubmissions.patch with a field mask containing the appropriate fields.
Here is the example code on how to do that.
studentSubmission = {
'assignedGrade': 99,
'draftGrade': 80
}
service.courses().courseWork().studentSubmission().patch(
courseId=<course ID or alias>,
courseWorkId=<courseWork ID>,
id=<studentSubmission ID>,
updateMask='assignedGrade,draftGrade',
body=studentSubmission).execute()

How to remove a value from the config in Trac

The config api within Trac is a pretty straight forward way of storing and loading settings from the config file. However, I haven't yet found a method to delete a line, or to delete all lines from a category that have no value assigned. So far I can only set the values to False or an empty string to deactivate them.
What I want to do is remove all values from my plugin's category that were set manually in the file and are not accepted by my script, or were set to False or no value by the script. In both ways, the whole line should be removed from the config so that it doesn't contain a lot of lines that look like this:
some_setting =
Edit
I just found out that I can assign None as value, which comments the setting, and the next time Trac writes to the config file it removes the commented line. This is a possible workaround, but I'm still interested in whether a direct method exists.
You can use Configuration.remove(...). Within a Component class call self.config.remove('section', 'key').

How to use ReplaceRows from .NET Google.Apis.Fusiontables.v2 (stream csv)?

Goal: to update a Fusion Table by replacing old rows by new ones from a csv file without headers using ReplaceRows().
I am using the Google.Apis.Fusiontables.v2 library.
I have read and reread the documentation, but still can`t get my code working.
Authentication is working and I am able to perform simple INSERTs without issue:
string sql = "INSERT INTO 11t9VLt3vzb46oGQMaS2LTSPWUyBYNcfi1shkmvag (rpu_id, NO_BAIL, 'Usage (description)', 'Use (description)', 'Sup. louable m2', 'Sup. Utilisable m2', 'SumTotal Lou', 'Percent Lou', 'SumTotal Util', 'Percent Util') VALUES (9999,1111,'Test','Test En',1,2,3,4,5,6)"
Sqlresponse sqlRspnse = service.Query.Sql(sql).Execute();
I have tried ReplaceRowsMediaUpload and ReplaceRowsMediaUpload directly from the TableResource class without luck.
Calling the upload function from the service object doesn't error out, but I'm not sure what to do next that would actually replace the rows in the Fusion Table (service is a FusiontablesService):
StreamReader str = new StreamReader(Server.MapPath("~") + #"\sample2.csv");
service.Table.ReplaceRows("1X7JMLFy75uq20UnU6cLrGTTDfp6lLuD1Fc3vYYjQ", str.BaseStream, "text/csv").Upload();
I've tried:
service.Table.ReplaceRows("1X7JMLFy75uq20UnU6cLrGTTDfp6lLuD1Fc3vYYjQ").Execute()
following the upload, but this just puts the Fusion table in "stuck" mode.
Can someone please provide the lines required to make ReplaceRows work? (Explanations would be appreciated, but aren't necessary!).
You should change "text/csv" for "application/octet-stream". (See accepted MIME type here: https://developers.google.com/fusiontables/docs/v2/reference/table/replaceRows)
StreamReader str = new StreamReader(Server.MapPath("~") + #"\sample2.csv");
service.Table.ReplaceRows("1X7JMLFy75uq20UnU6cLrGTTDfp6lLuD1Fc3vYYjQ", str.BaseStream, "application/octet-stream").Upload();
The call to Upload should be enough.
Also, try to create a new table to test it out, to be sure it is setup correctly.
You can use a REST API call to replace a row in your Google Fusion table directly instead of writing methods to do that. Here is an example:
POST https://www.googleapis.com/upload/fusiontables/v2/tables/tableId/replace
Please refer to this document for more details, it has a testing environment tool too.