-1

Using the script here I am able to import the sheet to another document however it does not work if I remove the columns from the srcRange. My goal is to copy the entire sheets formating over to the other sheet and the number of columns occasionally changes;

This works but the columns have to be specified:

  const srcSpreadsheetId = "1mVlva8Dyxxxxxxxxxxxxxx"; // Please set source Spreadsheet ID.
  const dstSpreadsheetId = "1a2Eb7fQOxxxxxxxxxxxxxx"; // Please set destination Spreadsheet ID.
  const srcRange = "Database!A:I";
  const dstRange = "Database";

  // Here, the date object is retrieved as the serial number.
  const values = Sheets.Spreadsheets.Values.get(srcSpreadsheetId, srcRange, { dateTimeRenderOption: "SERIAL_NUMBER", valueRenderOption: "UNFORMATTED_VALUE" }).values;

  const dstSheet = SpreadsheetApp.openById(dstSpreadsheetId).getSheetByName(dstRange);
  const sheetId = dstSheet.getSheetId();
  Sheets.Spreadsheets.batchUpdate({ requests: [{ repeatCell: { range: { sheetId }, fields: "userEnteredValue" } }] }, dstSpreadsheetId);
  Sheets.Spreadsheets.Values.update({ values }, dstSpreadsheetId, dstRange, { valueInputOption: "USER_ENTERED" });

  // Here, the number format is copied.
  const numberFormats = SpreadsheetApp.openById(srcSpreadsheetId).getRange(srcRange).getNumberFormats();
  dstSheet.getRange(1, 1, numberFormats.length, numberFormats[0].length).setNumberFormats(numberFormats);

This does not work:

  const srcSpreadsheetId = "1mVlva8Dyxxxxxxxxxxxxxx"; // Please set source Spreadsheet ID.
  const dstSpreadsheetId = "1a2Eb7fQOxxxxxxxxxxxxxx"; // Please set destination Spreadsheet ID.
  const srcRange = "Database";  // <<<<<<<<<<<<<<<<<   Columns not specified
  const dstRange = "Database";

  // Here, the date object is retrieved as the serial number.
  const values = Sheets.Spreadsheets.Values.get(srcSpreadsheetId, srcRange, { dateTimeRenderOption: "SERIAL_NUMBER", valueRenderOption: "UNFORMATTED_VALUE" }).values;

  const dstSheet = SpreadsheetApp.openById(dstSpreadsheetId).getSheetByName(dstRange);
  const sheetId = dstSheet.getSheetId();
  Sheets.Spreadsheets.batchUpdate({ requests: [{ repeatCell: { range: { sheetId }, fields: "userEnteredValue" } }] }, dstSpreadsheetId);
  Sheets.Spreadsheets.Values.update({ values }, dstSpreadsheetId, dstRange, { valueInputOption: "USER_ENTERED" });

  // Here, the number format is copied.
  const numberFormats = SpreadsheetApp.openById(srcSpreadsheetId).getRange(srcRange).getNumberFormats();
  dstSheet.getRange(1, 1, numberFormats.length, numberFormats[0].length).setNumberFormats(numberFormats);

Have tried multiple variations to no avail. Keep getting > Exception: Range not found.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • First, I apologize that my answer was not useful for your situation. About your error of `Keep getting > Exception: Range not found.`, I have to apologize for my poor English skill. Unfortunately, I couldn't know the error line in your showing script. So, where the line does the error occurs? – Tanaike Oct 28 '22 at 04:14
  • Your script is great @Tanaike! Just need a slight change for what I need. The error is in: `const numberFormats = SpreadsheetApp.openById(srcSpreadsheetId).getRange(srcRange).getNumberFormats(); ` The source range only works right now if you put in the columns but I want to copy the format for the entire sheet. – Micah Noble Oct 28 '22 at 04:18
  • 1
    Thank you for replying. About `The source range only works right now if you put in the columns but I want to copy the format for the entire sheet.`, in your situation, when you use `const srcRange = "Database!A:IX";` to `const numberFormats = SpreadsheetApp.openById(srcSpreadsheetId).getRange(srcRange).getNumberFormats()`, an error of `Exception: Range not found.` occurs. Is my understanding correct? – Tanaike Oct 28 '22 at 04:23
  • No error > `const srcRange = "Database!A:IX";`, error happens if I use > `const srcRange = "Database";` – Micah Noble Oct 28 '22 at 04:25
  • Thank you for replying. From your reply, I proposed 2 modification points in an answer. Could you please confirm it? – Tanaike Oct 28 '22 at 04:29
  • If I don't specify A:IX I get the error. – Micah Noble Oct 28 '22 at 04:32
  • Thank you for replying. About `If I don't specify A:IX I get the error.`, I have to apologize for my poor English skill. From `The source range only works right now if you put in the columns but I want to copy the format for the entire sheet.` and `No error > const srcRange = "Database!A:IX";, error happens if I use > const srcRange = "Database";`, I proposed using `const srcRange = "Database"`. Unfortunately, I have not proposed to use `A:IX`. Could you please confirm my answer? If you cannot understand my answer, please tell me. – Tanaike Oct 28 '22 at 04:34
  • If you use `const srcRange = "A:IX";`, "A:IX" of the 1st tab is used. Please be careful about this. – Tanaike Oct 28 '22 at 04:37
  • Can you share a sample/copy of your spreadsheet? Please remove sensitive data if any. – Logan Oct 28 '22 at 06:24
  • I have made a copy that can be viewed and changed. (https://docs.google.com/spreadsheets/d/1TLllvZu5m245nVGJAyFLbZUT1-u033ewrxHbBdssI9c/edit?usp=sharing) – Micah Noble Oct 28 '22 at 20:44

1 Answers1

2

If you want to achieve The source range only works right now if you put in the columns but I want to copy the format for the entire sheet. and your actual error is No error > const srcRange = "Database!A:IX";, error happens if I use > const srcRange = "Database";, how about the following modification?

From:

const srcRange = "Database!A:IX";

To:

const srcRange = "Database";

And,

From:

const numberFormats = SpreadsheetApp.openById(srcSpreadsheetId).getRange(srcRange).getNumberFormats();

To:

const numberFormats = SpreadsheetApp.openById(srcSpreadsheetId).getSheetByName(srcRange).getDataRange().getNumberFormats();

or

const srcSheet = SpreadsheetApp.openById(srcSpreadsheetId).getSheetByName(srcRange);
const numberFormats = srcSheet.getRange(1, 1, srcSheet.getMaxRows(), srcSheet.getMaxColumns()).getNumberFormats();

Added:

From I tested both the first and second solution you suggested with a smaller amount of data and both work. The only wierd part is that for numbers stored as text they are in a white font and you cannot change the color., when I saw your Spreadsheet, I understood that the reason for this issue is due to plain text format. When the cells of plain text are retrieved with getNumberFormats(), it seems that null` is returned. By this, the number is not displayed. In order to reflect this issue, please modify it as follows.

Modified script:

Please set the Spreadsheet IDs and sheet names for your test situation.

function sample() {
  const srcSpreadsheetId = "1mVlva8Dyxxxxxxxxxxxxxx"; // Please set source Spreadsheet ID.
  const dstSpreadsheetId = "1a2Eb7fQOxxxxxxxxxxxxxx"; // Please set destination Spreadsheet ID.
  const srcRange = "Database";  // <<<<<<<<<<<<<<<<<   Columns not specified
  const dstRange = "Database";

  // Here, the date object is retrieved as the serial number.
  const values = Sheets.Spreadsheets.Values.get(srcSpreadsheetId, srcRange, { dateTimeRenderOption: "SERIAL_NUMBER", valueRenderOption: "UNFORMATTED_VALUE" }).values;

  const dstSheet = SpreadsheetApp.openById(dstSpreadsheetId).getSheetByName(dstRange);
  const sheetId = dstSheet.getSheetId();
  Sheets.Spreadsheets.batchUpdate({ requests: [{ repeatCell: { range: { sheetId }, fields: "userEnteredValue" } }] }, dstSpreadsheetId);
  Sheets.Spreadsheets.Values.update({ values }, dstSpreadsheetId, dstRange, { valueInputOption: "USER_ENTERED" });

  // Here, the number format is copied.
  const srcSheet = SpreadsheetApp.openById(srcSpreadsheetId).getSheetByName(srcRange);
  const range = srcSheet.getRange(1, 1, srcSheet.getMaxRows(), srcSheet.getMaxColumns());
  const numberFormats = range.getNumberFormats().map(r => r.map(c => c || "@"));
  const styles = range.getTextStyles();
  dstSheet.getRange(1, 1, numberFormats.length, numberFormats[0].length).setNumberFormats(numberFormats).setTextStyles(styles);
}
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • both keep timing out, may be that I've got too many programs running though – Micah Noble Oct 28 '22 at 05:03
  • @Micah Noble Thank you for replying. About `both keep timing out, may be that I've got too many programs running though`, unfortunately, I cannot replicate your situation. When I tested this script, no error occurs. I apologize for this. But, I would like to support you. So, can you provide detailed information for correctly replicating it? By this, I would like to confirm it. – Tanaike Oct 28 '22 at 05:04
  • @Micah Noble From `both keep timing out, may be that I've got too many programs running though`, it is found that your error of `Exception: Range not found.` was resolved. About your new issue, although I'm not sure about your actual situation, in this case, in order to test the script, please test the script using a simple Spreadsheet. First, I would like to recommend confirming whether the script works using the modified script. – Tanaike Oct 28 '22 at 05:09
  • @Micah Noble For example, when the number of cells is large, the general methods for using Spreadsheet cannot be directly used. [Ref](https://tanaikech.github.io/2022/04/25/report-handling-10000000-cells-in-google-spreadsheet-using-google-apps-script/) So, I thought that first, it confirms whether your modified script works. And then, as the next step, if you want to use a large number of cells, you prepare the script for your actual situation. In this case, I would like to recommend separating your question from this question. If I misunderstood your actual situation, I apologize. – Tanaike Oct 28 '22 at 05:13
  • Wasn't able to get it to work. Both versions timed out. I have made a copy that can be viewed and changed. (https://docs.google.com/spreadsheets/d/1TLllvZu5m245nVGJAyFLbZUT1-u033ewrxHbBdssI9c/edit?usp=sharing) – Micah Noble Oct 28 '22 at 20:42
  • I tested both the first and second solution you suggested with a smaller amount of data and both work. The only wierd part is that for numbers stored as text they are in a white font and you cannot change the color. Not sure why. The smaller data set I used is: (https://docs.google.com/spreadsheets/d/1WJBGDq92XjUchOSvAB7ltHz84-eTB-KxpPA8GjSXZ-I/edit?usp=sharing). However when trying to import the actual data which is 4853 rows and 206 columns, both scripts timeout. – Micah Noble Oct 28 '22 at 21:41
  • you are correct in that the script works with smaller data with the exception being the issue of numbers stored as text being formated in white. Should I still accept the answer and then start a new post for the issues with the script timing out? – Micah Noble Oct 28 '22 at 21:44
  • @Micah Noble Thank you for replying and testing `I tested both the first and second solution you suggested with a smaller amount of data and both work.`. About the new issue of `The only wierd part is that for numbers stored as text they are in a white font and you cannot change the color. Not sure why.`, I added one more modified script in my answer. Could you please confirm it? – Tanaike Oct 28 '22 at 22:44
  • @Micah Noble And, about `Should I still accept the answer and then start a new post for the issues with the script timing out?`, I think that it is yes because I'm worried that when your actual situation is used, the script might be required to be modified. So, first, I would like to recommend to resolve from your initial question. – Tanaike Oct 28 '22 at 22:44
  • marking the original suggest as correct as it works with the one wierd font issue. Your last update didn't work for me as it removed the numders stored as text entirely just leaving a blank space. – Micah Noble Nov 01 '22 at 03:23
  • @Micah Noble Thank you for replying and testing it. From your reply, I understood that your issue was resolved. I'm glad your issue was resolved. About ` the issues with the script timing out`, when you posted it as a new question, I would like to confirm it. – Tanaike Nov 01 '22 at 03:32