How to Fix "Error: Please specify a valid Spreadsheet URL." - google-apps-script

I'm using a stock Google Apps Script for Link Checker (https://developers.google.com/google-ads/scripts/docs/solutions/link-checker#source-code). I've copied the template spreadsheet to reference but I keep getting response "Error: Please specify a valid Spreadsheet URL."
The part of the code that seems to be the issue is the 3rd row below:
function validateAndGetSpreadsheet(spreadsheeturl) {
if (spreadsheeturl == 'YOUR_SPREADSHEET_URL') {
throw new Error('Please specify a valid Spreadsheet URL. You can find' +
' a link to a template in the associated guide for this script.');
I've input my URL to replace the 'YOUR_SPREADSHEET_URL' but am not sure what I'm supposed to input into the
'Please specify a valid Spreadsheet URL. You can find' +
' a link to a template in the associated guide for this script.'
I tried putting the my spreadsheet URL in there, and also I tried leaving it out, but neither works. I am a complete Google Scripts novice so any help would be gratefully received!

You need to have changed the URL parameter on here:
var CONFIG = {
SPREADSHEET_URL: 'YOUR_SPREADSHEET_URL',

Related

How to open an url and save a file sent as response (excel) directly to google drive with google script

Hello and thanks for your time.
Below you will find a link (request) for the data/excel file that I would like to save directly to my google drive and in the future import to google sheets:
https://digital.feprecisionplus.com/corppen/en-GB/cp-am/DownloadTool/GetPriceHistoryFile?modelString={%22GrsProjectId%22:%2236800083%22,%22ProjectName%22:%22corppen%22,%22ToolId%22:16,%22LanguageId%22:%221%22,%22LanguageCode%22:%22en-GB%22,%22OverrideDocumentCountryCode%22:null,%22forSaleIn%22:%22%22,%22FSIexclCT%22:%22%22}&filtersString={%22TypeCode%22:%22FA:N6SQ%22,%22FundName%22:%22JLR%20Bond%22,%22BaseCurrency%22:%22GBX%22,%22PriceType%22:1,%22TimePeriod%22:%22%22,%22StartDate%22:%22Mon%20Mar%2001%202021%22,%22EndDate%22:%22Thu%20Mar%2004%202021%22}
I tried google script below ( "my_URL" to be replaced with the link above)
function uploadFile() {
var blob = UrlFetchApp.fetch("my_URL").getBlob()
file = DriveApp.getFolderById("my_FolderID").createFile(blob);
}
but I am getting "Exception: Invalid argument: "my_URL"" response.
Would anyone be able to help me with getting that file saved to drive?
Cheers
Modification points:
When I tested your script using your URL, no error occurs. The XLSX file could be created. But from but I am getting "Exception: Invalid argument: "my_URL"" response., I thought that you might have used https://digital.feprecisionplus.com/corppen/en-GB/cp-am/DownloadTool/GetPriceHistoryFile?modelString={%22GrsProjectId%22:%2236800083%22,%22ProjectName%22:%22corppen%22,%22ToolId%22:16,%22LanguageId%22:%221%22,%22LanguageCode%22:%22en-GB%22,%22OverrideDocumentCountryCode%22:null,%22forSaleIn%22:%22%22,%22FSIexclCT%22:%22%22}&filtersString={%22TypeCode%22:%22FA:N6SQ%22,%22FundName%22:%22JLR%20Bond%22,%22BaseCurrency%22:%22GBX%22,%22PriceType%22:1,%22TimePeriod%22:%22%22,%22StartDate%22:%22Mon%20Mar%2001%202021%22,%22EndDate%22:%22Thu%20Mar%2004%202021%22} as the URL. If it's so, such error occurs.
In this case, please test the URL of https://digital.feprecisionplus.com/corppen/en-GB/cp-am/DownloadTool/GetPriceHistoryFile?modelString=%7B%22GrsProjectId%22:%2236800083%22,%22ProjectName%22:%22corppen%22,%22ToolId%22:16,%22LanguageId%22:%221%22,%22LanguageCode%22:%22en-GB%22,%22OverrideDocumentCountryCode%22:null,%22forSaleIn%22:%22%22,%22FSIexclCT%22:%22%22%7D&filtersString=%7B%22TypeCode%22:%22FA:N6SQ%22,%22FundName%22:%22JLR%20Bond%22,%22BaseCurrency%22:%22GBX%22,%22PriceType%22:1,%22TimePeriod%22:%22%22,%22StartDate%22:%22Mon%20Mar%2001%202021%22,%22EndDate%22:%22Thu%20Mar%2004%202021%22%7D.
{ and } are encoded to %7B and %7D, respectively. (I thought that this might be the reason of your issue.)
When above points are reflected to your script, it becomes as follows.
Modified script:
function uploadFile() {
var url = "https://digital.feprecisionplus.com/corppen/en-GB/cp-am/DownloadTool/GetPriceHistoryFile?modelString=%7B%22GrsProjectId%22:%2236800083%22,%22ProjectName%22:%22corppen%22,%22ToolId%22:16,%22LanguageId%22:%221%22,%22LanguageCode%22:%22en-GB%22,%22OverrideDocumentCountryCode%22:null,%22forSaleIn%22:%22%22,%22FSIexclCT%22:%22%22%7D&filtersString=%7B%22TypeCode%22:%22FA:N6SQ%22,%22FundName%22:%22JLR%20Bond%22,%22BaseCurrency%22:%22GBX%22,%22PriceType%22:1,%22TimePeriod%22:%22%22,%22StartDate%22:%22Mon%20Mar%2001%202021%22,%22EndDate%22:%22Thu%20Mar%2004%202021%22%7D";
var blob = UrlFetchApp.fetch(url).getBlob();
file = DriveApp.getFolderById("my_FolderID").createFile(blob);
}
Before you use this script, please replace my_FolderID with your folder ID.

How to easily refresh all functions in a google sheets spreadsheet? (like deleting the function and entering it again)

I hope you're well.
I'm using google sheets to help manage some data for my work. One of our programmers wrote a google apps script for me to log into our website, pull the data I need, and then dump it into google sheets. It works well, however sometimes it encounters and error and it fails to log in to our website. (please see the photo below)
Error I encounter
To fix the error, I need to delete the cell's contents, and then enter the function in that cell again. This forces the function to try again from scratch and it will load successfully.
What I cut and paste to fix it
What I need a way to automatically run all the functions again (something functionally the same as deleting it and entering it again manually).
I'm hoping for someway to create a button that I could press which would delete and re-enter the text in the relevant cells, or something like that. However, I would appreciate any solution that anyone can suggest.
I really appreciate it!
-edit: I should also mention that I'm using this function a lot in a large range of spreadsheets and they're all interconnected so when one gets an error, it kills the entire web of documents and I need to root around for which one failed and then copy its contents, delete it, then enter it again.
edit 2: Thank you fir your first response and sorry about posting images of the error I received. You've set me down the right path and I've combined this onEdit function for when I edit single sell to directly replace the text in that cell. When I use "foo" or something like that, it works and replaces the text in that sell with "foo". However, what I really need to replace it with is =ImportJSON("expertList", 1) but when I try i get an error:
Syntax error: SyntaxError: Unexpected identifier line: 341 file: Code.gs
Here's the code in question:
function onEdit(e) {
if (e.range.getA1Notation() === 'A1') {
refresher()
}
}
function refresher() {
var newValue = '=yourfunction()';
var sheetName = "sheet name";
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
var range = sheet.getRange("this is the cell you want to re-write");
range.clearContent();
SpreadsheetApp.flush();
range.setValue(newValue);
}
Last Edit:
Thanks for the help! If anyone stumbles on this post in the future, the above code works to delete what's in cell, then enter it again into that same cell (thereby resetting the function)

my script "GET URL from google document" does not work any more - server failure WHY?

In general I want to print the URL of the google document I am actually working with in a text field.
If I copy the google sheet the function should also work for the new document.
I scripted a solution which was working for 20 month and now I have a problem.
Now I would like to know what could happen? Why do I have a server problem? Is is it possible to have to many google documents, (i have ~9000 - it works as a small ERP system) that the server got problems?
Formula in Google sheet:
=hyperlink(getSheetUrl()
Code in Script editor:
function getSheetUrl() {
var SS = SpreadsheetApp.getActiveSpreadsheet();
var ss = SS.getActiveSheet();
var url = '';
url += SS.getUrl();
url += '#gid=';
url += ss.getSheetId();
return url;
}
This is the error message in my document:
Error: Server error occurred. Please try saving the project again. (Row 0).
If I try to save the project in my script editor I get no failure.
If I try to run the project in my script editor I get:
A server error has occurred. Please try to save the project again.
Thanks for your answers.
Today I started the system and everything works fine.
Yesterday i was also able to run my code in a new enviroment, but i was not able to copy a existing document. there i got the failure.
Maybe it was really because the lan connection was to weak.
My company is a very rural area ;)
I will take an eye on that,
By the way.
Does anyone has another idea how to get the link from a document and print it in a textfield in the same document (hopefully you know what i mean)

Google Apps forms Script openById , openByUrl is not working

I have started to learn Google App Scripting and was stuck at the first example using the Forms Api.
Following is my code:
function testFunction(){
var newClientForm = FormApp.openById('1f-676zsIuVpsQVSr1tAxttTs********');
var clientForm = FormApp.openByUrl('Url of the form in edit mode');
Logger.log('The form Title is ', newClientForm.getTitle());
Logger.log('The form Title is ', clientForm.getTitle());
}
Either way when I check the logs I only get the string message with blank values.
To check that my form id is correct I have tried deleted new characters in the Id and the script reports an error. But when the correct Id is mentioned the script does not work as expected.
Any inputs would be greatly appreciated.
I have spotted my mistake
The issues was with the Logger.log() method which i was incorrectly using. I should have mentioned as
Logger.log('The form Title is %s', newClientForm.getTitle());
I was missing the formatter '%s'

How to reference GmailApp in a js file other than Code.gs?

Is it possible to reference GmailApp in a my_custom_js.html file, as opposed to the Code.gs file?
The following works when used in Code.gs:
// BEGIN email
// define email recipients
var email_recipient = uploader_email;
// Email subject
var subject = "Form submitted"
// Email body
var body = my_html;
Logger.log(email_recipient);
// Send email
GmailApp.sendEmail(email_recipient, subject, body);
// END email
But it doesn't work when used in a function in my_custom_js.html.
Developer Tools > Console shows error:
Uncaught ReferenceError: Logger is not defined
Uncaught ReferenceError: GmailApp is not defined
The reason I want it to run in the custom script, is that it utilises:
// BEGIN handle form submit
function handleFormSubmit(formObject) {
google.script.run.withSuccessHandler(updateUrl).processForm(formObject);
}
// END handle form submit
And, if I understand that code correctly, updateUrl is only running after a successful operation - so I'd like to send the email from the updateUrl() function (ie after a successful operation).
Perhaps I need to use something like a scriptlet , but for use in js files?
Edit
I'm looking into:
https://developers.google.com/apps-script/guides/html/reference/run
And will see if I can pass through the values to a function defined in Code.gs using the following in my_custom_js.html:
google.script.run.sendNotificationEmail(arg1,arg2);
Edit
That last idea worked, but would appreciate any insight on the original question, thanks.
This week for the first time I have been using Google's realatively new Javascript Client API. Using this api you could trigger your Google apps script to run from any custom website and get a callback with the result from any methods you would normally use with in the appscript. It took a bit of fiddling to get it to work, including setting up things in API console, but it's so cool you can run and call and trigger any appscript from anywhere.