Skip to main content
February 8, 2023
Question

Dynamic text - changing text in html5

  • February 8, 2023
  • 2 replies
  • 398 views

Hi,

 

I'm trying to make a programmatic/dynamic banner with textfields that can be changed through a excel file. I want always the same spacing (15px) below the textfields even when the text, that is inserted via excel, is longer than the original one. At the moment the textfields are on top of each other when I insert a longer textfield than the original one. 

 

Can I change that somehow or add code in the html or javascript so it shows the text underneath and with spacing?

 

Here is the preview for the banner if that helps:

https://livepreview.adform.com/68559935-1d33-48b1-bd18-98ce863ae5d5/

 

Any kind of help is very much appreciated!

This topic has been closed for replies.

2 replies

JoãoCésar17023019
Community Expert
Community Expert
February 9, 2023

Hi.

 

As there some things that I don't understand in your file, I created a simple sample that i hope will help you to get started.

 

Javascript:

// The SheeJS library is included in the 'Include' section to the left
// https://cdn.sheetjs.com/xlsx-0.19.2/package/dist/xlsx.full.min.js

var xlsxFileName = "test.xlsx";
var sheetName = "Sheet1"; // you can get this name from the console.log(workbook) call below
var rows = [ "1", "2" ];
var targetTextFieldsContainers;

function main()
{
	root.stop();
	targetTextFieldsContainers = [ root.tf0, root.tf1, root.tf2, root.tf3, root.tf4 ];
	readXLSX(xlsxFileName, onXLSXLoaded, { sheetName: sheetName, rows: rows });
}

function readXLSX(src, callback, props)
{
	var req = new XMLHttpRequest();
	var textFields = [];
	var texts = [];
	var data = [];
	var i;

	req.open("GET", src, true);
	req.responseType = "arraybuffer";

	req.onload = function(e)
	{
		var workbook = XLSX.read(req.response);
		var worksheet = workbook.Sheets[props.sheetName];
		var key;
		
		console.log(workbook);
		console.log(worksheet);

		for (key in worksheet)
		{
			if (key.indexOf(props.rows[0]) > -1)
				textFields.push(worksheet[key].h);

			if (key.indexOf(props.rows[1]) > -1)
				texts.push(worksheet[key].h);
		}

		for (i = 0; i < textFields.length; i++)
			data[i] = { tf: textFields[i], text: texts[i] };

		callback(data);
	};

	req.send();
}

function onXLSXLoaded(data)
{
	console.log(data);
	
	data.forEach(function(item)
	{
		if (targetTextFieldsContainers.indexOf(root[item.tf]) > -1)
			root[item.tf].tf.text = item.text;
	});

	stackTextFields(targetTextFieldsContainers);
}

function stackTextFields(targets)
{
	targets.forEach(function(tf, index, array)
	{
		var currentHeightOffset = tf.tf.getMeasuredHeight() * 0.5;
		
		tf.tf.y = -currentHeightOffset;
		
		if (index > 0)
		{
			var previousTF = array[index - 1];
			tf.y = previousTF.y + previousTF.tf.getMeasuredHeight() * 0.5 + currentHeightOffset;
		}
	});
}

if (!this.looped)
{
	window.root = this;
	main();
	root.looped = 1;
}

 

In your case the sheet name is Tabelle1.

 

Source / files / FLA / code:
https://bit.ly/3DW426o

 

I hope it helps.

Regards,
JC

JoãoCésar17023019
Community Expert
Community Expert
February 8, 2023

Hi.

 

I think I don't get it.

 

In the sample you provided, there's a table that uses regular DOM texts, right? And the table seems to be looking correctly.

 

So exactly what do you want to do in that preview? And can you show us the code you have in Animate now?

 

Regards,

JC

February 8, 2023

Hi, 

thank you for your answer! I use dynamic textfields in Animate. 

If you click the second version you'll see what I mean. Here is a screenshot:  

 

Do you want me to send the animate file? I added code in animate and the html...

 

Thank you so much for your help again! 

Regards,

Adele

JoãoCésar17023019
Community Expert
Community Expert
February 8, 2023

Thanks for providing more details.

 

I don't see a link to another version but if you can upload your FLA to somewhere like Creative Cloud, Google Drive or WeTransfer, and post the link here, it will certainly be very helpful.

 

Regards,

JC