App Script encounters a service error while attempting to transfer data between spreadsheets

My goal is to transfer data from one spreadsheet to another. The code should ideally retrieve data from one spreadsheet, save it into a 2D array, and then print all the data into another spreadsheet. However, I am encountering an error during the printing process that reads "Exception: Service error: Spreadsheets."

var pasteSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
uploadFolder = DriveApp.getFolderById('someID')
obtainAndImportData(uploadFolder)

function obtainAndImportData(uploadFolder){
  try{
    var internalFiles = uploadFolder.getFiles()
  } catch{
    return
  }
  while (internalFiles.hasNext()){
    try{
      var file = internalFiles.next()
    } catch {
      break
    }
    var fileID = file.getId()
    var copySheet = SpreadsheetApp.openById(fileID).getSheets()[0]

    var Cvals = copySheet.getRange("C1:C" + copySheet.getLastRow()).getValues()
    var ldr = Cvals.length;
    var Csheet = pasteSheet.getRange("C1:C" + pasteSheet.getLastRow()).getValues()
    var lstv = Csheet.length;

    var allRows = []
    for (i = 0;i < ldr;i++){
      allRows.push(copySheet.getRange(`B${i + 3}:P${i + 3}`).getValues()[0])
    }

    console.log(allRows)

    var rangeToUnify = pasteSheet.getRange(lstv + 1,1,allRows.length,allRows[0].length)
    rangeToUnify.setValues(allRows) // this line throws the error
  }
}

I have researched that this issue may be related to large amounts of data, but even testing with just one line causes the same failure.

The data consists of columns from A to P and any number of rows. It's worth mentioning that the original file format is ".xslx," which is later converted to a Google Sheets file. https://i.sstatic.net/3e2AV.png

Answer №1

When running the obtainAndImportData function from the Apps Script editor, it's important to remove

obtainAndImportData(uploadFolder)
from the global scope. This is necessary because calling a function from the global scope causes it to be executed every time any function is run. In this scenario, your function will run twice - once when the code is loaded and again when the selected function is executed in the Apps Script editor dropdown.


To prevent this behavior, move the variable declarations that call Google Apps Script services out of the global scope. Specifically, move
var pasteSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
uploadFolder = DriveApp.getFolderById('someID')

just below

function obtainAndImportData(uploadFolder){

For more information on variable scopes in JavaScript, see:

  • What is the scope of variables in JavaScript?

Answer №2

After some investigation, I discovered that the error consistently pops up whenever an image is detected within a cell. Since images are considered objects and cannot be seamlessly copied from one cell to another.

A workaround would involve simply bypassing cells where this issue occurs, and strictly transferring numeric and text values across spreadsheets. Below is my approach to address it:

    for (i = 0; i < allRows.length; i++){ // Swap out allRows with a 2d array
      for (j = 0; j < allRows[i].length; j++){
        var cellType = typeof allRows[i][j]
        if (cellType == "object"){
          allRows[i].splice(j,1,"Object type element")
          }
      }
    }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Firebase firestore is reporting an error stating that the requested document cannot be found

Using nodejs, I am working on creating firestore documents in a specific structure that includes the collection name followed by an ID and then the document itself. While my code successfully adds the document, it does not create the parent document (ID) i ...

Exploring the functionality of Material-UI's TextField component through role-based testing with React

I currently have some components that contain mui TextFields, and there are two specific scenarios for these components: One TextField is meant for LicenseCode and does not require a label. Additionally, there are several TextFields generated using t ...

Leveraging a variable in Python for XPATH in Selenium

I have a variable that looks like this: client_Id = driver.execute_script("return getCurrentClientId()") I want to update the XPATH by replacing the last value (after clientid=2227885) with the client_Id variable. So: prog_note = wait.until(EC.p ...

What is the process of extracting data from JavaScript output using Mongoose?

Currently facing an issue with mongoose while working on a blog website and integrating quilljs. The problem arises from the output file in my JavaScript file that contains the configurations for my quill editor. var quill = new Quill('#editor&ap ...

Refreshing Information in Angular

I am attempting to create a reload data button. Here is the JSON structure I am working with: [ { "name": "AAAAAA", "data": "False" }, { "name": "BBBBBB", "data": "45%" ...

I'm experiencing an issue where the graph seems to vanish whenever I dynamically add edges using vis-network.js. What could

I've encountered a strange issue in my Vue project while using vis-network.js - the graphics disappear when I dynamically add edges, but dynamic updates and deletes work fine. To further investigate, I created a test page where I included vis-network ...

Uncovering the Mystery Behind the Repetitive Execution of useEffect in Next.js

I am working on a Time Tracking feature for my Next.js application. In the ./app/components/TimeTracking.tsx file, I have implemented the following component: 'use client'; import React, { useEffect, useState } from 'react'; import { u ...

The data in Next.js getStaticProps does not update or refresh as expected

As I recently transitioned to working with Next.js, I have encountered several challenges. One issue that arose was related to the use of useEffect in React compared to its behavior in Next.js. In my index file, I have implemented the following code: impo ...

Angular CDK Overlay allows for bringing multiple overlays to the front effectively

Currently, I am experiencing an issue with Angular 15 where a click event placed within a mousedown event does not trigger. Interestingly, if the position of the element is not changed using appendChild, both the mousedown and click events work as expected ...

Can anyone explain to me how to render attributes of a tag in Vue? I'm curious about how v-for interacts with HTML

<ul> <span class="tabs" :class="{ activeTab: selectedTab === tab }" v-for="(tab, index) in tabs" @click="selectedTab = tab" :key="tab"> {{ in ...

Determine the occurrences of each element in a given array and save them as key-value pairs

Is there a way to efficiently convert an array of elements into an object with key value pairs representing element frequencies? For example, given an array: var array = ['a','a','a','b','b','c&ap ...

Updating state of an array nested inside an object using React and ES6

I'm fairly new to Javascript and I feel like my lack of fundamental knowledge might be hindering me from solving this issue. Despite trying different approaches and reading tutorials, I can't seem to get it right. My goal is to manipulate the st ...

Modifying webpage design using JavaScript for styling

Is there a way to change the css style(defined in the page source) dynamically with Java? I know it is possible to do it with JavaScript. If there isn't, are there other situations where JavaScript is the only choice developing a web app? ...

JavaScript array contains duplicate values

I have developed a custom Asp.Net user control. To keep things simple, I have omitted some of the HTML code. <asp:Panel ID="PanelInputControl" runat="server" CssClass="input-control input-period"> <div ID="InputWrapperMonth" runat="server" c ...

iOS Safari browser does not support changing the caret color in textarea

Seeking a solution to hide the text cursor (caret) from a textarea on iOS browsers like Safari and Chrome. Despite trying the caret-color property, it does not seem to work. Are there any alternative methods to achieve this? One approach I attempted is b ...

Ways to avoid a function from being repeatedly executed in an addEventListener

I am looking to initiate an animation when the user hovers their cursor over an HTML element that contains multiple nested elements. Consider the code snippet below: <div ref={exampleReference}> <img src='...' /> <div> & ...

Transmitting information after a delay when using ng-keyup in AngularJS

In my Angular app, I am using ng-keyup to search data from my backend. However, the issue is that it sends a request to the server with every key press. Is there a way to configure it to send a post request only after the user has paused or stopped typing? ...

Gradually appearing/disappearing scrolling menu

After creating a script to implement a sticky menu on scroll, I encountered a bounce "bug" and decided to make it fade in/out for a smoother effect. However, I'm struggling to get it to work properly... This is the HTML code: <headnav> < ...

"Step-by-step guide on adding a new row to a table using a child controller in AngularJS

I have a table where I can insert new rows by opening a dialog window and entering the data. The dialog window contains a child controller called addNewTaskCtrl. Inside this dialog, there is a form that inserts data into the table when the "Add" button is ...

What is the best method to verify chrome.runtime.onInstalled in Chrome extension testing?

Is there a way to test the chrome.runtime.onInstalled handler? I am developing a Chrome extension that utilizes chrome storage to store specific data. In my upcoming release, I plan on making changes to the data model in chrome storage. To do this effectiv ...