Cannot add row to spreadsheet - google-sheets

var ssNew = SpreadsheetApp.open('Student Files Data').getId();
var ss = SpreadsheetApp.openById(ssNew);
var sheet = ss.getSheetByName('Sheet1');
sheet.appendRow(["a man", "a plan", "panama"]);
I can't seem to get this to work. What am I missing?

The issue is the first line; the open() method expects a file object, not the file name as a string. Use the DriveApp getFilesByName method, which will return a file iterator, and next() will return the first match.
var ssNew = DriveApp.getFilesByName("Student Files Data").next().getId;

Related

Parse XML Feed via Google Apps Script (Cannot read property 'getChildren' of undefined")

I need to parse a Google Alert RSS Feed with Google Apps Script.
Google Alerts RSS-Feed
I found a script which should do the job but I cant get it working with Google's RSS Feed:
The feed looks like this:
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:idx="urn:atom-extension:indexing">
<id>tag:google.com,2005:reader/user/06807031914929345698/state/com.google/alerts/10604166159629661594</id>
<title>Google Alert – garbe industrial real estate</title>
<link href="https://www.google.com/alerts/feeds/06807031914929345698/10604166159629661594" rel="self"/>
<updated>2022-03-17T19:34:28Z</updated>
<entry>
<id>tag:google.com,2013:googlealerts/feed:10523743457612307958</id>
<title type="html"><b>Garbe Industrial</b> plant Multi-User-Immobilie in Ludwigsfelde - <b>Property</b> Magazine</title>
<link href="https://www.google.com/url?rct=j&sa=t&url=https://www.property-magazine.de/garbe-industrial-plant-multi-user-immobilie-in-ludwigsfelde-117551.html&ct=ga&cd=CAIyGWRmNjU0ZGNkMzJiZTRkOWY6ZGU6ZGU6REU&usg=AFQjCNENveXYlfrPc7pZTltgXY8lEAPe4A"/>
<published>2022-03-17T19:34:28Z</published>
<updated>2022-03-17T19:34:28Z</updated>
<content type="html">Die <b>Garbe Industrial Real Estate</b> GmbH startet ihr drittes Neubauprojekt in der Metropolregion Berlin/Brandenburg. Der Projektentwickler hat sich ...</content>
<author>
...
</feed>
I want to extract entry -> id, title, link, updated, content.
I used this script:
function ImportFeed(url, n) {
var res = UrlFetchApp.fetch(url).getContentText();
var xml = XmlService.parse(res);
//var item = xml.getRootElement().getChild("channel").getChildren("item")[n - 1].getChildren();
var item = xml.getRootElement().getChildren("entry")[n - 1].getChildren();
var values = item.reduce(function(obj, e) {
obj[e.getName()] = e.getValue();
return obj;
}, {});
return [[values.id, values.title, values.link, values.updated, values.content]];
}
I modified this part, but all i got was "TypeError: Cannot read property 'getChildren' of undefined"
//var item = xml.getRootElement().getChild("channel").getChildren("item")[n - 1].getChildren();
var item = xml.getRootElement().getChildren("entry")[n - 1].getChildren();
Any idea is welcome!
In your situation, how about the following modified script?
Modified script:
function SAMPLE(url, n = 1) {
var res = UrlFetchApp.fetch(url).getContentText();
var root = XmlService.parse(res.replace(/&/g, "&")).getRootElement();
var ns = root.getNamespace();
var entries = root.getChildren("entry", ns);
if (!entries || entries.length == 0) return "No values";
var header = ["id", "title", "link", "updated", "content"];
var values = header.map(f => f == "link" ? entries[n - 1].getChild(f, ns).getAttribute("href").getValue().trim() : entries[n - 1].getChild(f, ns).getValue().trim());
return [values];
}
In this case, when you use getChild and getChildren, please use the name space. I thought that this might be the reason of your issue.
From your script, I guessed that you might use your script as the custom function. In that case, please modify the function name from ImportFeed to others, because IMPORTFEED is a built-in function of Google Spreadsheet. In this sample, SAMPLE is used.
If you want to change the columns, please modify header.
In this sample, the default value of n is 1. In this case, the 1st entry is retrieved.
In this script, for example, you can put =SAMPLE("URL", 1) to a cell as the custom function. By this, the result value is returned.
Note:
If the above-modified script was not the direct solution of your issue, can you provide the sample value of res? By this, I would like to modify the script.
As the additional information, when you want to put all values by executing the script with the script editor, you can also use the following script.
function myFunction() {
var url = "###"; // Please set URL.
var res = UrlFetchApp.fetch(url).getContentText();
var root = XmlService.parse(res.replace(/&/g, "&")).getRootElement();
var ns = root.getNamespace();
var entries = root.getChildren("entry", ns);
if (!entries || entries.length == 0) return "No values";
var header = ["id", "title", "link", "updated", "content"];
var values = entries.map(e => header.map(f => f == "link" ? e.getChild(f, ns).getAttribute("href").getValue().trim() : e.getChild(f, ns).getValue().trim()));
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1"); // Please set the sheet name.
sheet.getRange(sheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
}
References:
XML Service
map()

Google script: search and replace the url of linked text in a Doc (not sheet)

I am trying to search-and-replace linked text from an old url to a new url.
It is not working and I have spent hours and hours. If I remove the "if (found)" it gives me "TypeError: Cannot read property 'getElement' of null" even though my files have text that is linked to this old_url.
Please, help me.
function myFunction() {
var old_url ="http://hurlx1.com";
var new_url ="http://urlxa.com";
var files = DriveApp.getFolderById("my folder id").getFilesByType(MimeType.GOOGLE_DOCS);
while (files.hasNext()) {
var file = files.next();
var doc = DocumentApp.openById(file.getId());
found=doc.getBody().findText(old_url);
if (found) {
var link_element = found.getElement().asText();
var start = found.getStartOffset();
var end = found.getEndOffsetInclusive();
var correct_link = link_element.getText().slice(start, end);
link_element.setLinkUrl(start, end, correct_link);
}
}
}
I believe your situation and goal as follows.
In your Google Document,
Text and hyperlink are the same with old_url.
Hyperlink is old_url. But the text is different from old_url.
You want to update old_url with new_url using Google Apps Script.
For this, how about this answer?
Modification points:
About your error message, when the text of old_url is not found in the Google Document with found=doc.getBody().findText(old_url);, found becomes null even when old_url is set as the hyperlink. Because findText searches the text on Document body, and that cannot search the hyperlinks set to the texts. I think that this is the reason of your issue.
In your script, var new_url ="http://urlxa.com"; is used. But when the link is set, correct_link is used like link_element.setLinkUrl(start, end, correct_link);. By this, new_url is not set.
When you want to update the text of http://hurlx1.com to new_url of var new_url ="http://urlxa.com";, it is required to also modify the text.
In your script, only the 1st old_url is updated. If there are several values of old_url in the Document, it is required to update them using the loop.
Specification of modified script:
This modified script can be used for the following patterns.
Text and hyperlink are the same with old_url.
In this case, the text value of old_url is also updated with old_url.
Hyperlink is old_url. But the text is different from old_url.
In this case, only the hyperlink of old_url is updated.
There are several texts with the hyperlink of old_url in the Google Document.
Modified script:
function myFunction() {
var old_url ="http://hurlx1.com";
var new_url ="http://urlxa.com";
var files = DriveApp.getFolderById("my folder id").getFilesByType(MimeType.GOOGLE_DOCS);
while (files.hasNext()) {
var file = files.next();
var doc = DocumentApp.openById(file.getId());
var body = doc.getBody();
// The following script is used for the situation that the text and hyperlink are the same with `old_url`.
var found = body.findText(old_url);
while (found) {
var link_element = found.getElement().asText();
var start = found.getStartOffset();
var end = found.getEndOffsetInclusive();
var correct_link = link_element.getText().slice(start, end);
link_element.setLinkUrl(start, end, new_url).replaceText(old_url, new_url);
found = body.findText(old_url, found);
}
// The following script is used for the situation that although the hyperlink is `old_url`, the text is different from `old_url`.
var text = body.editAsText();
for (var i = 0; i < text.getText().length; i++) {
if (text.getLinkUrl(i) == old_url) {
text.setLinkUrl(i, i + 1, new_url);
}
}
}
}
References:
replaceText(searchPattern, replacement)
findText(searchPattern, from)
editAsText()

Google script stops working after copying and authorizing

After making a copy of my perfectly working google sheet, together with the script that comes with it; the script itself does not work anymore in the copy.
I go through the steps of authorizing it as an app but it seems to hang when i run it on the copy. It times out after 540 odd seconds. It seems to be having trouble with DriveApp.createFolder (exectution time 540 seconds). Yet i have not changed anything in the script, works fine on the original one.
Here is the script, im pretty sure everything is fine here, it just doesn't want to work as soon as you make a copy of it. I need to make copies of this sheet for each person in my team but i can't at the moment.
function saveAsCSV() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var date = SpreadsheetApp.getActiveSheet().getRange(3,2).getValue();
var time = SpreadsheetApp.getActiveSheet().getRange(4,2).getValue();
var site = SpreadsheetApp.getActiveSheet().getRange(2,2).getValue();
// iterate through all sheets in the spreadsheet and rename them according to cell B2
for( var j = 0 ; j < sheets.length; j++) {
var sourceSheet = sheets[j];
// get contents of cell B2
var newSheetName = sourceSheet.getRange("B2").getValue();
// rename sheet
sourceSheet.setName(newSheetName);
}
// create a folder from the named SNOWSURVEYS with date
var folder = DriveApp.createFolder('SNOWSURVEYS' + '_' + date + '_'+ site);
// append ".csv" extension to the sheet name
fileName = SpreadsheetApp.getActiveSheet().getName() + ".csv";
// convert all available sheet data to csv format
var csvFile = convertRangeToCsvFile_(fileName);
// create a file in the Docs List with the given name and the csv data
folder.createFile(fileName, csvFile);
var folderID = folder.getId();
var folderName = folder.getName();
var ui = UiApp.createApplication().setTitle('Your files have are in your google drive in a folder called ' + folder);
var p = ui.createVerticalPanel();
ui.add(p);
p.add(ui.createAnchor('Click here to go straight to your files ',folder.getUrl()));
SpreadsheetApp.getActive().show(ui)
}
Execution transcript printscreen

TypeError: Cannot find function getSheets in object - looking for what's wrong

While running this code, there is error while executing this line:
var invoiceSheet = newSSFile.getSheets()[0];'
"TypeError: Cannot find function getSheets in object Copy of Invoice Przykładowy. (line 69, file "Code")"
With this code I want:
Create a new spreadsheet and move it to proper folder [works]
Get a value from another spreadsheet and paste it in this new one [error]
Looked for answer for an hour without any result. Any idea what might cause this error?
function invoice() {
//Create copy SS + name
var ssTemp = SpreadsheetApp.openById("1Cr2W_4lNrHYRdXK-KDQ7UFJJ3Iagh-tGct8Ee5Y");
var newSS = ssTemp.copy("Copy of " + ssTemp.getName());
// Move to folder
var DestinyFolder = DriveApp.getFolderById("0B-y1OC8ChG2XRjRRbEJ");
var newSSFile = DriveApp.getFileById(newSS.getId());
DestinyFolder.addFile(newSSFile);
DriveApp.getRootFolder().removeFile(newSSFile);
// Modify details
// Invoice No
var klienciSS = SpreadsheetApp.openById("18B151VlJaVtDdQ9CcLrL3iwRAtWw2ZzZydproj");
var klienciSheet = klienciSS.getSheets()[0];
var klienciRange= klienciSheet.getRange('AB6');
var klienciValue = klienciRange.getValue();
var invoiceSheet = newSSFile.getSheets()[0];
var inboiceRange = invoiceSheet.getRange('F4');
newCellInvoice.setValue(klienciValue);
The problem is caused by the following line
var newSSFile = DriveApp.getFileById(newSS.getId());
It makes that newSSFile holds an instance of Class File instead of an instance of Class Spreadsheet
But the problematic line looks unnecessary. Replace the line that throws the error by
var invoiceSheet = newSS.getSheets()[0];

Google Apps Script

I've created a simple script that reads through an xml file and posts the results to an SQL database. This works perfectly.
I've put a little if statement in the script to identify orders that have already been posted to SQL. Basically if the transactionID in the input array is higher than the highest transactionID on the SQL server it adds the row values to the output array.
It seems that I am missing a trick here because I am getting "TypeError: Cannot call method "getAttribute" of undefined. (line 18, file "Code")" when trying to compare the current xml row to the last transaction ID.
I've done some searching and whilst I can see people with similar problems the explanations don't make a whole lot of sense to me.
Anyway, here is the relevant part of the code. Note that this all works perfectly without the if() bit.
function getXML() {
var id = lastTransactionID();
var xmlSite = UrlFetchApp.fetch("https://api.eveonline.com/corp/WalletTransactions.xml.aspx?KeyID=1111&vCode=1111&accountKey=1001").getContentText();
var xmlDoc = XmlService.parse(xmlSite);
var root = xmlDoc.getRootElement();
var row = new Array();
row = root.getChild("result").getChild("rowset").getChildren("row");
var output = new Array();
var i = 0;
for (j=0;i<row.length;j++){
if(row[j].getAttribute("transactionID").getValue()>id){ //Produces: TypeError: Cannot call method "getAttribute" of undefined. (line 18, file "Code")
output[i] = new Array();
output[i][0] = row[j].getAttribute("transactionDateTime").getValue();
output[i][1] = row[j].getAttribute("transactionID").getValue();
output[i][2] = row[j].getAttribute("quantity").getValue();
output[i][3] = row[j].getAttribute("typeName").getValue();
output[i][4] = row[j].getAttribute("typeID").getValue();
output[i][5] = row[j].getAttribute("price").getValue();
output[i][6] = row[j].getAttribute("clientID").getValue();
output[i][7] = row[j].getAttribute("clientName").getValue();
output[i][8] = row[j].getAttribute("stationID").getValue();
output[i][9] = row[j].getAttribute("stationName").getValue();
output[i][10] = row[j].getAttribute("transactionType").getValue();
output[i][11] = row[j].getAttribute("transactionFor").getValue();
output[i][12] = row[j].getAttribute("journalTransactionID").getValue();
output[i][13] = row[j].getAttribute("clientTypeID").getValue();
i++;
}
}
insert(output,output.length);
}
I have seen my mistake and corrected.
Mistake was in the for loop.
for (j=0;i

Resources